如何将静态属性添加到ES6类

Abd*_*UMI 3 javascript oop static attributes ecmascript-6

我们非常清楚地知道class的ES6也带来了:static,get以及set功能:

但是,似乎static关键字仅保留给方法:

class Person {

    // static method --> No error
    static size(){
    }   
  // static attribute --> with Error
    static MIN=10;
}
Run Code Online (Sandbox Code Playgroud)

如何static在ES6类中编写属性以获得类似静态属性的内容MIN.

我们知道我们可以在类定义后添加以下指令:

Person.MIN=10; 
Run Code Online (Sandbox Code Playgroud)

但是,我们的范围是找到在类块中编写此指令的方法

Mor*_*ani 8

你可以使用静态getter:

class HasStaticValue {
  static get MIN() {
    return 10;
  }
}

console.log(HasStaticValue.MIN);
Run Code Online (Sandbox Code Playgroud)