我们非常清楚地知道class
的ES6也带来:static
,get
以及set
功能:
但是,似乎static
关键字仅保留给Methods:
class Person {
// static method --> No error
static size(){
}
// static attribute --> with Error
static MIN=10;
}
如何能够static
在ES6类中编写属性以具有诸如static属性之类的东西MIN
。
我们知道我们可以在类定义之后添加以下指令:
Person.MIN=10;
但是,我们的范围是找到在类块中编写此指令的方法
您可以使用静态吸气剂:
class HasStaticValue {
static get MIN() {
return 10;
}
}
console.log(HasStaticValue.MIN);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句