Are there constants in JavaScript?

后端 未结 30 2891
抹茶落季
抹茶落季 2020-11-22 08:53

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条回答
  •  执笔经年
    2020-11-22 09:29

    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?

提交回复
热议问题