Javascript ES6类中成员变量的声明
我在ES6中见过这样声明的成员变量Javascript ES6类中成员变量的声明,javascript,ecmascript-6,Javascript,Ecmascript 6,我在ES6中见过这样声明的成员变量 export class MyClass { x = null; constructor() { this.x = 1; } write() { console.log(this.x); } } 巴贝尔似乎把它转得很好 这是声明成员变量的有效方法吗?我认为这是不对的。至少,报告没有提到任何这样的语法 至于你的例子,让我们一行一行地看一遍 class MyClass { // Class declar
export class MyClass
{
x = null;
constructor() {
this.x = 1;
}
write() {
console.log(this.x);
}
}
巴贝尔似乎把它转得很好
这是声明成员变量的有效方法吗?我认为这是不对的。至少,报告没有提到任何这样的语法 至于你的例子,让我们一行一行地看一遍
class MyClass { // Class declaration, all good here
x = null; // I assume you're telling javascript that the variable x exists?
constructor() {
this.x = 1; // You do that here just fine.
}
write() {
console.log(this.x); // And here we use the variable, after the constructor ran
}
}
我认为单独声明成员变量没有任何价值。您可以在构造函数中创建它。这应该是您所需要的全部我认为这是不对的。至少,报告没有提到任何这样的语法 至于你的例子,让我们一行一行地看一遍
class MyClass { // Class declaration, all good here
x = null; // I assume you're telling javascript that the variable x exists?
constructor() {
this.x = 1; // You do that here just fine.
}
write() {
console.log(this.x); // And here we use the variable, after the constructor ran
}
}
我认为单独声明成员变量没有任何价值。您可以在构造函数中创建它。这应该是您所需要的全部了这是您的建议的一部分。 它是由babeljs支持的。
这是一个babel stage-1插件,因此,如果您使用的是stage-1或stage-0,则支持此插件。这是建议的一部分。 它是由babeljs支持的。 这是一个babel stage-1插件,因此如果您使用的是stage-1或stage-0,则支持此插件。JS中没有“成员变量”这类东西。您看到的不是ES6,而是ES8的实验性功能建议。不要使用它。JS中没有“成员变量”这样的东西。您看到的不是ES6,而是ES8的实验性功能建议。不要用它。