Are there constants in JavaScript?

2018-12-31 13:46发布

Is there a way to use constants in JavaScript?

If not, what's the common practice for specifying variables that are used as constants?

30条回答
旧时光的记忆
2楼-- · 2018-12-31 14:37

with the "new" Object api you can do something like this:

var obj = {};
Object.defineProperty(obj, 'CONSTANT', {
  configurable: false
  enumerable: true,
  writable: false,
  value: "your constant value"
});

take a look at this on the Mozilla MDN for more specifics. It's not a first level variable, as it is attached to an object, but if you have a scope, anything, you can attach it to that. this should work as well. So for example doing this in the global scope will declare a pseudo constant value on the window (which is a really bad idea, you shouldn't declare global vars carelessly)

Object.defineProperty(this, 'constant', {
  enumerable: true, 
  writable: false, 
  value: 7, 
  configurable: false
});

> constant
=> 7
> constant = 5
=> 7

note: assignment will give you back the assigned value in the console, but the variable's value will not change

查看更多
零度萤火
3楼-- · 2018-12-31 14:38

Another alternative is something like:

var constants = {
      MY_CONSTANT : "myconstant",
      SOMETHING_ELSE : 123
    }
  , constantMap = new function ConstantMap() {};

for(var c in constants) {
  !function(cKey) {
    Object.defineProperty(constantMap, cKey, {
      enumerable : true,
      get : function(name) { return constants[cKey]; }
    })
  }(c);
}

Then simply: var foo = constantMap.MY_CONSTANT

If you were to constantMap.MY_CONSTANT = "bar" it would have no effect as we're trying to use an assignment operator with a getter, hence constantMap.MY_CONSTANT === "myconstant" would remain true.

查看更多
皆成旧梦
4楼-- · 2018-12-31 14:39

Group constants into structures where possible:

Example, in my current game project, I have used below:

var CONST_WILD_TYPES = {
    REGULAR: 'REGULAR',
    EXPANDING: 'EXPANDING',
    STICKY: 'STICKY',
    SHIFTING: 'SHIFTING'
};

Assignment:

var wildType = CONST_WILD_TYPES.REGULAR;

Comparision:

if (wildType === CONST_WILD_TYPES.REGULAR) {
    // do something here
}

More recently I am using, for comparision:

switch (wildType) {
    case CONST_WILD_TYPES.REGULAR:
        // do something here
        break;
    case CONST_WILD_TYPES.EXPANDING:
        // do something here
        break;
}

IE11 is with new ES6 standard that has 'const' declaration.
Above works in earlier browsers like IE8, IE9 & IE10.

查看更多
孤独总比滥情好
5楼-- · 2018-12-31 14:39

Clearly this shows the need for a standardized cross-browser const keyword.

But for now:

var myconst = value;

or

Object['myconst'] = value;

Both seem sufficient and anything else is like shooting a fly with a bazooka.

查看更多
路过你的时光
6楼-- · 2018-12-31 14:42

in Javascript already exists constants. You define a constant like this:

const name1 = value;

This cannot change through reassignment.

查看更多
皆成旧梦
7楼-- · 2018-12-31 14:44

ECMAScript 5 does introduce Object.defineProperty:

Object.defineProperty (window,'CONSTANT',{ value : 5, writable: false });

It's supported in every modern browser (as well as IE ≥ 9).

See also: Object.defineProperty in ES5?

查看更多
登录 后发表回答