Constants of object properties in ECMAScript?

I saw some suggestions for ECMAScript Harmony in terms of being able to define constants with a keyword const. However, it is apparently only available in block regions (i.e., FunctionBodyand Program). Is there a way to define constants as an object property (represent myObj.STATUS_OK)?

I guess the proposed method is to use propertiesthrough Object.definePropert(y/ies), but it doesn’t seem very convenient, now is it?

+3
source share
2 answers

It seems to be constmore related to the bindings of environment frames and, thus, is a slightly different beast to objects and properties.

, globals, :

const a = 10;
Object.getOwnPropertyDescriptor(window, "a");
/*
   Object:
      configurable: true,
      enumerable: true
      value: 10
      writable: false
*/

, .

:

var myObj = {
   get STATUS_OK(){ return 42; }
};
+6

.

:

var CONST = 
 {
   ONE: 1,
   TWO: 2
 };

Object.seal(CONST);

:

   CONST.ONE, CONST.TWO 
+1

All Articles