第二行显示错误。
"ReferenceError: specialTrick is not defined
at CoolGuy.showoff (<anonymous>:23:40)
at <anonymous>:31:5
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
at Object.InjectedScript.evaluate (<anonymous>:664:21)"
class CoolGuy {
specialTrick = null;
CoolGuy( trick ) {
specialTrick = trick
}
showOff() {
console.log( "Here's my trick: ", specialTrick );
}
}
Joe = new CoolGuy("rope climbing");
Joe.shoeOff();
答案 0 :(得分:5)
constructor
函数(而不是具有相同名称的函数)。this
在类定义中设置成员(在构造函数中设置它们)。showOff
函数中有错字。更多信息in the reference。
以下是修复:
class CoolGuy {
constructor( trick ) {
this.specialTrick = trick
}
showOff() {
console.log( "Here's my trick: ", this.specialTrick );
}
}
Joe = new CoolGuy("rope climbing");
Joe.showOff();