Monday, April 21, 2014

enums in JavaScript!

Make them like so:

var obj = Object.freeze({ "foo": 0, "bar": 1, "baz": 2, "qux": 3 });

 
 

Use them like so:

var whatever = obj.foo;

 
 

Object.freeze() locks the shape of an object so that new stuff may not be bolted onto it.

No comments:

Post a Comment