我想在类中实现常量,因为在代码中定位它们是有意义的。
到目前为止,我一直在用静态方法实现以下工作:
class MyClass {
static constant1() { return 33; }
static constant2() { return 2; }
// ...
}
我知道有可能会摆弄原型,但许多人建议不要这样做。
在ES6类中有更好的实现常量的方法吗?
我想在类中实现常量,因为在代码中定位它们是有意义的。
到目前为止,我一直在用静态方法实现以下工作:
class MyClass {
static constant1() { return 33; }
static constant2() { return 2; }
// ...
}
我知道有可能会摆弄原型,但许多人建议不要这样做。
在ES6类中有更好的实现常量的方法吗?
当前回答
也可以在类(es6)/构造函数(es5)对象上使用object .freeze,使其不可变:
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
试图改变类会给你一个软失败(不会抛出任何错误,它根本没有效果)。
其他回答
我使用巴别塔和以下语法是为我工作:
class MyClass {
static constant1 = 33;
static constant2 = {
case1: 1,
case2: 2,
};
// ...
}
MyClass.constant1 === 33
MyClass.constant2.case1 === 1
请考虑您需要预设的“stage-0”。 安装方法:
npm install --save-dev babel-preset-stage-0
// in .babelrc
{
"presets": ["stage-0"]
}
更新阶段:
它被移到了第三阶段。
更新通天塔7:
根据巴别塔7阶段预设已弃用。
要使用的Babel插件是@babel/plugin-proposal-class-properties。
npm i --save-dev @babel/plugin-proposal-class-properties
{
"plugins": ["@babel/plugin-proposal-class-properties"]
}
注意:这个插件包含在@babel/preset-env中
加上其他答案,您需要导出类以在不同的类中使用。这是它的typescript版本。
/ / Constants.tsx const DEBUG: boolean = true; 导出类常量{ static get DEBUG(): boolean { 返回调试; } } / / Anotherclass.tsx import {Constants} from "Constants"; if (Constants.DEBUG) { console.log(“调试模式”) }
也可以在类(es6)/构造函数(es5)对象上使用object .freeze,使其不可变:
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
试图改变类会给你一个软失败(不会抛出任何错误,它根本没有效果)。
这里有一些你可以做的事情:
从模块中导出一个const。根据你的用例,你可以:
export const constant1 = 33;
并在必要时从模块中导入。或者,基于你的静态方法思想,你可以声明一个静态get访问器:
const constant1 = 33,
constant2 = 2;
class Example {
static get constant1() {
return constant1;
}
static get constant2() {
return constant2;
}
}
这样,你就不需要括号了:
const one = Example.constant1;
Babel REPL示例
然后,正如你所说,由于类只是函数的语法糖,你可以添加一个不可写的属性,如下所示:
class Example {
}
Object.defineProperty(Example, 'constant1', {
value: 33,
writable : false,
enumerable : true,
configurable : false
});
Example.constant1; // 33
Example.constant1 = 15; // TypeError
如果我们能做些这样的事情就好了:
class Example {
static const constant1 = 33;
}
但不幸的是,这个类属性语法只在ES7提案中,即使这样,它也不允许将const添加到属性中。
只需将变量声明为私有并使用get方法检索它们。
class MyClass {
#myConst = 'Something';
static #anotherConst = 'Something Else';
get myConst() {
return this.#myConst; // instance method
}
static get anotherConst() {
return MyClass.#anotherConst; // static method
}
}
let myClass = new MyClass();
console.log( myClass.myConst + ' is not ' + MyClass.anotherConst );
用户不能更改原始变量,您可以编写类来使用get方法而不是私有变量本身。