blackhole://nilFM

immutable javascript objects

enum

We can simulate enums with a stringly typed object:

  const Colors = {
    RED: 'red',
    BLUE: 'blue',
    GREEN: 'green'
  }

And then deep freeze and export it:

((o) => {
  Object.freeze(o);

  Object.getOwnPropertyNames(o).forEach(function (prop) {
    if (o.hasOwnProperty(prop)
    && o[prop] !== null
    && (typeof o[prop] === "object" || typeof o[prop] === "function")
    && !Object.isFrozen(o[prop])) {
      deepFreeze(o[prop]);
    }
  });
  
  return o;
})(Colors)

export { Colors }