首先,我在开发工具中编写以下代码,并且一切正常:
class MyClass {
constructor(name, verb, noun) {
this.name = name;
this.verb = verb;
this.noun = noun;
}
myFunc() {
return `${this.name}${this.verb}${this.noun}`
}
}
但是,如果我将其更改为:
class MyClass {
constructor(name, verb, noun) {
this.name = name;
this.verb = verb;
this.noun = noun;
}
myFunc = () => {
return `${this.name}${this.verb}${this.noun}`
}
}
只需将传统功能转换为箭头功能,它会告诉我:
Uncaught SyntaxError: Unexpected token =
我不能在课堂上写箭头功能吗?