I work on a pretty large Angular project everyday at work that has a very polluted global namespace due to what I think are meant to be “constants”. Actually, the are just variables defined on the window object in all caps that contain a lot of strings and some numbers meant to be used as codes and other things like that. They could be overwritten at anytime so this is just a disaster waiting to happen.
Object.defineProperty() is a very interesting ES5 method that allows you to add properties to an object than can be writable, enumerable, and configurable. By default when you use defineProperty it makes properties that are not writable (writable and configurable is set to false) which to us means read only and is exactly what we want.
Here is an example of this in action. This object has a method for adding properties that can be come constants, not on the global namespace, and we can then freeze this container when we are done if we like.