While reading up about housekeeping the global object in ECMAscript 6, I
noticed a subtlety of
const that could have escaped the attention of some
software developers like myself so I’m writing this post especially to
help me remember … because I need to be careful for the seemingly immutable, yet capable of change.
Non-primitives such as arrays and objects are accessed through their pointers and constants to such items pertain to the reference and not the data referred to.
A constant array or object therefore always maintains its reference1, but the data pointed to by the immutable reference may be mutated.
In the case of an array one may mutate the object referred to by use of the
unshift functions as demonstrated in
the following snippet:
The data referred to by an object reference may also be mutated:
Upon first glance it isn’t far fetched to assume that somewhere in the life cycle of a codebase, someone will mistake a constant for an item of immutable state. . Here is how I maintain sanity:
- remember that primitives are passed by value
- remember that non-primitives are passed by a copy of the reference2
- always return copies to non-primitives, ensuring that no one has access to the data I need for my housekeeping, effectively giving me some guarantees regarding state.
- Dr. Axel Rauschmayer’s Variables and scoping in ECMAScript 6