Ale*_*lex 0 javascript ecmascript-6
我想做的是创建一个变量,我可以在类中的不同函数之间使用。但是由于某种原因,每当我let variable在构造函数上方编写代码时,我都会得到'Unexpected token. A constructor, method, accessor, or property was expected.
用var尝试过,我得到的结果差不多
class ClassName {
let variable;
constructor() {
variable = 1;
}
function() {
console.log(variable + 1);
}
}Run Code Online (Sandbox Code Playgroud)
您应该将变量作为的属性进行访问this:
class ClassName {
constructor() {
this.variable = 1;
}
someOtherFunction() {
console.log(this.variable + 1); // 2
}
}
new ClassName().someOtherFunction();Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1288 次 |
| 最近记录: |