Jér*_*nge 272 javascript class constants ecmascript-6
我想在a中实现常量class,因为在代码中找到它们是有意义的.
到目前为止,我一直在使用静态方法实现以下解决方法:
class MyClass {
static constant1() { return 33; }
static constant2() { return 2; }
// ...
}
Run Code Online (Sandbox Code Playgroud)
我知道有可能摆弄原型,但许多人建议不要这样做.
有没有更好的方法在ES6类中实现常量?
Cod*_*gue 342
这里有一些你可以做的事情:
const从模块导出a .根据您的使用情况,您可以:
export const constant1 = 33;
Run Code Online (Sandbox Code Playgroud)
并在必要时从模块导入.或者,基于静态方法的想法,您可以声明一个static get访问器:
const constant1 = 33,
constant2 = 2;
class Example {
static get constant1() {
return constant1;
}
static get constant2() {
return constant2;
}
}
Run Code Online (Sandbox Code Playgroud)
这样,你就不需要括号:
const one = Example.constant1;
Run Code Online (Sandbox Code Playgroud)
然后,如你所说,因为a class只是函数的语法糖,你可以添加一个不可写的属性,如下所示:
class Example {
}
Object.defineProperty(Example, 'constant1', {
value: 33,
writable : false,
enumerable : true,
configurable : false
});
Example.constant1; // 33
Example.constant1 = 15; // TypeError
Run Code Online (Sandbox Code Playgroud)
如果我们可以做以下事情可能会很好:
class Example {
static const constant1 = 33;
}
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,此类属性语法仅在ES7提案中,即使这样也不允许添加const到属性.
bor*_*Blu 23
我正在使用babel,以下语法对我有用:
class MyClass {
static constant1 = 33;
static constant2 = {
case1: 1,
case2: 2,
};
// ...
}
MyClass.constant1 === 33
MyClass.constant2.case1 === 1
Run Code Online (Sandbox Code Playgroud)
请考虑您需要预设"stage-0".
要安装它:
npm install --save-dev babel-preset-stage-0
// in .babelrc
{
"presets": ["stage-0"]
}
Run Code Online (Sandbox Code Playgroud)
更新:
目前使用 stage-3
Ben*_*gan 22
class Whatever {
static get MyConst() { return 10; }
}
let a = Whatever.MyConst;
Run Code Online (Sandbox Code Playgroud)
似乎为我工作.
Dev*_*ien 13
在本文件中,它指出:
(故意)没有直接声明方式来定义原型数据属性(方法除外)类属性或实例属性
这意味着故意这样.
也许你可以在构造函数中定义一个变量?
constructor(){
this.key = value
}
Run Code Online (Sandbox Code Playgroud)
rod*_*tti 10
也可以Object.freeze在你的类(es6)/构造函数(es5)对象上使用它来使它不可变:
class MyConstants {}
MyConstants.staticValue = 3;
MyConstants.staticMethod = function() {
return 4;
}
Object.freeze(MyConstants);
// after the freeze, any attempts of altering the MyConstants class will have no result
// (either trying to alter, add or delete a property)
MyConstants.staticValue === 3; // true
MyConstants.staticValue = 55; // will have no effect
MyConstants.staticValue === 3; // true
MyConstants.otherStaticValue = "other" // will have no effect
MyConstants.otherStaticValue === undefined // true
delete MyConstants.staticMethod // false
typeof(MyConstants.staticMethod) === "function" // true
Run Code Online (Sandbox Code Playgroud)
试图改变类将给你一个软失败(不会抛出任何错误,它将没有任何效果).
您可以使用 ES6 类的一个奇怪功能创建一种在类上定义静态常量的方法。由于静态由其子类继承,因此您可以执行以下操作:
const withConsts = (map, BaseClass = Object) => {
class ConstClass extends BaseClass { }
Object.keys(map).forEach(key => {
Object.defineProperty(ConstClass, key, {
value: map[key],
writable : false,
enumerable : true,
configurable : false
});
});
return ConstClass;
};
class MyClass extends withConsts({ MY_CONST: 'this is defined' }) {
foo() {
console.log(MyClass.MY_CONST);
}
}
Run Code Online (Sandbox Code Playgroud)
也许只是将您的所有常量都放在冻结的对象中?
class MyClass {
constructor() {
this.constants = Object.freeze({
constant1: 33,
constant2: 2,
});
}
static get constant1() {
return this.constants.constant1;
}
doThisAndThat() {
//...
let value = this.constants.constant2;
//...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208357 次 |
| 最近记录: |