-2 javascript function referenceerror
我有以下问题:我想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 definedRun Code Online (Sandbox Code Playgroud)
使用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 definedRun Code Online (Sandbox Code Playgroud)