我想在类中实现常量,因为在代码中定位它们是有意义的。
到目前为止,我一直在用静态方法实现以下工作:
class MyClass {
static constant1() { return 33; }
static constant2() { return 2; }
// ...
}
我知道有可能会摆弄原型,但许多人建议不要这样做。
在ES6类中有更好的实现常量的方法吗?
我想在类中实现常量,因为在代码中定位它们是有意义的。
到目前为止,我一直在用静态方法实现以下工作:
class MyClass {
static constant1() { return 33; }
static constant2() { return 2; }
// ...
}
我知道有可能会摆弄原型,但许多人建议不要这样做。
在ES6类中有更好的实现常量的方法吗?
当前回答
如果你喜欢混合和匹配函数和类的语法,你可以在类之后声明常量(常量被“提升”)。注意,Visual Studio Code将很难自动格式化混合语法(尽管它可以工作)。
MyClass { / /…… } MyClass.prototype.consts = { constant1: 33岁 constant2: 32 }; mc = new MyClass(); console.log (mc.consts.constant2);
其他回答
在这份文件中,它指出:
(有意地)没有直接的声明性方法来定义原型数据属性(方法除外)类属性或实例属性
这意味着它是故意这样的。
也许你可以在构造函数中定义一个变量?
constructor(){
this.key = value
}
您可以使用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);
}
}
加上其他答案,您需要导出类以在不同的类中使用。这是它的typescript版本。
/ / Constants.tsx const DEBUG: boolean = true; 导出类常量{ static get DEBUG(): boolean { 返回调试; } } / / Anotherclass.tsx import {Constants} from "Constants"; if (Constants.DEBUG) { console.log(“调试模式”) }
这里有一些你可以做的事情:
从模块中导出一个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添加到属性中。
给你!
const Status = Object.freeze(class Status {
static Disabled = 0
static Live = 1
})