javascript - 如何从类中的另一个函数调用函数
问题描述
我有以下问题:我想printHello
从函数调用函数testHello
。该printHello
函数独立工作,但是,当我尝试printHello
从该testHello
函数调用时,我得到一个引用错误。谢谢您的帮助。
class Test {
constructor(name) {
this.name;
}
printHello(parameter) {
console.log(parameter);
}
testHello() {
printHello(printHello(this.name));
}
}
var test = new Test("Sandro");
test.printHello("hello"); //works, prints "Hello" to the Console
test.testHello(); // does not work: Reference Error: printHello is not defined
解决方案
使用this
关键字。另外,你有一些错误(我评论了他们)
class Test{
constructor(name){
this.name = name; // <- you need to assign the `name` to `this.name`
}
printHello(parameter){
console.log(parameter);
}
testHello(){
this.printHello(this.name); // <- you had double invocation here
}
}
var test = new Test("Sandro");
test.printHello("hello"); //works, prints "Hello" to the Console
test.testHello(); // does not work: Reference Error: printHello is not defined
推荐阅读
- spring-boot - 在 Spring Boot logback-spring.xml 中创建自定义日志级别
- oracle - SymmetricDS:ORA-12899:SYM_NODE_GROUP_LINK.DATA_EVENT_ACTION 列的值太大
- javascript - 在 Web 浏览器或 Node 内部,为什么如果你不监听它就不会发生事件,但是一旦你监听它,它就会发生?
- gatsby - 如何在 Gatsby 的构建期间获取远程文件?
- java - 如何使用 Appium 与智能手机模拟器交互?
- kubernetes - 使用 varnish 从 EKS 中寻找有关 SSL 错误的调试建议
- permissions - 如何在 Windows 10 中授予 zsh 终端权限?
- c# - C# linq 和表达式
- c# - 无法在 Google Drive API C# 中下载 .zip 文件
- excel - 通过用户输入应用的 VBA 多个过滤器