[英]Replacing constants with some other in JavaScript
const Name = 'Name ';
const Name_USER_PASSWORD = 'Name ';
const Name= 'Name ';
const USER_FULL_NAME = 'FullName';
I have such 50 constants in my project, but i thought is there any better way to adapt this with some technique in javascript. 我的项目中有50个这样的常数,但是我认为有任何更好的方法可以通过javascript中的某种技术来适应它。 Like putting them in prototype or something else. 就像将它们放在原型或其他东西中一样。
I don't use the const
keyword to declare public constants, because the global namespace would be polluted. 我不使用const
关键字来声明公共常量,因为全局名称空间将被污染。
My recommended way of defining constants is using the Object.defineProperty
method, with a fallback to a property assignment method. 我推荐的定义常量的方法是使用Object.defineProperty
方法,并使用一个属性分配方法。 Additionally, I use special values for my constants: an object literal ( {}
). 另外,我为常量使用特殊值:对象文字( {}
)。 The purpose of this value is explained at the end of this answer. 该值的目的在此答案的结尾进行了说明。
// ... created a class, object, or somethin, say MyNS
// You could write a method to implement the following constant-definition method
if (Object.defineProperty) { // Yay, ES5!
Object.defineProperty(MyNS, 'CONST', {
value: {},
enumerable: true, // Default false. Are the props visible, eg in a for-loop?
writable: false, // Default false, constants should be CONSTANT
configurable: false// Default false, prevent deletion of the constant, etc.
});
} else { // If ES is not supported:
MyNS.CONST = {};
}
To check against a constant: 要检查一个常数:
if (setting === MyNS.CONSTANT) // .... Rest of logic
Using this method, the user cannot hard-code constant values (which can lead to unexpected, hard-to-debug errors). 使用此方法,用户无法对常量值进行硬编码(这可能会导致意外的,难以调试的错误)。
The comparison will always return false unless the variable is really pointing to MyNS.CONSTANT1
. 除非变量确实指向MyNS.CONSTANT1
否则比较将始终返回false。
An additional application is a function with a dynamic number of arguments. 另一个应用程序是带有动态数量的参数的函数。 Say, you want to create a kind of console.log
function, with configurable options. 假设您要创建一种console.log
函数,并带有可配置的选项。 Using string/number/... constant values will lead to possible errors. 使用字符串/数字/ ...常量值将导致可能的错误。
Example: 例:
var log.CONST = 1;
function log() {
for (var i=0; i<arguments.length; i++) {
if (arguments[i] === log.CONST) ...some logic...
}
}
log(1); // 1 is the value of log.CONST, the function will FAIL.
// Fix: If you were using log.CONST = {}, the assertion will only be true
// when the argument === log.CONST
If you're looking for a way to organise your constants you can make them properties of one of more objects, eg: 如果您正在寻找一种组织常量的方法,则可以使它们成为多个对象之一的属性,例如:
var myConstants = {
NAME : "name",
NAME_USER_PASSWORD : "password here",
USER_FULL_NAME : "Joe Jones",
ETC : "Other constant"
};
console.log( myConstants.USER_FULL_NAME ); // "Joe Jones"
Or a more complicated example with grouping: 或更复杂的分组示例:
var myConstants = {
DB : {
CONNECTION_STRING : "something",
USER : "name",
PASSWORD : "password here"
},
COLOR : {
BLACK : "#000000",
RED : "#FF0000",
WHITE : "#FFFFFF"
}
};
console.log( myConstants.DB.PASSWORD ); // "password here"
Of course myConstants
is a pretty lame name, but you get the idea... 当然, myConstants
是个很myConstants
名字,但是您知道了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.