我找不到关于如何使用(相对较新的)类语法在JavaScript中定义静态数据成员的任何信息。这个是否可能?请看下面的示例代码:
class Foo {
constructor() {
this.name = 'Default Name for each Instance';
// staticData1 = 'Static Data 1'; // syntax error
let staticData2 = 'Static Data 2'; // undefined outside
}
doSthSpecial() {
console.log('Executing a method from instance "' + this.name + '"');
}
static doSthStatic() {
console.log('Executing a method that does the same for each instance of "Foo"');
}
}
foo = new Foo();
console.log(foo.name);
foo.doSthSpecial();
Foo.doSthStatic();
// The problematic case:
console.log(Foo.staticData2);