Proxy with JavaScript

2017/10/01 code web

JavaScript getters and setters are implemented via the Proxy object. Here we use an object named stevedore[1] as a proxy to localStorage to transparently get and set persistent JSON data from localStorage.

Then, we can further restrict the possiblity of really messing with localStorage by ensuring that values in some way resemble data in a defaults object.

Really this is all just a sad substitute for types, but given what we are dealing with, I'd suggest that all the required type-checking is done in the getter function as opposed to the setter function.

If we are writing the code, we can be reasonably sure that all of our functions call our proxy object, but we cannot be sure that we are the only ones manipulating localStorage.

[1] stevedore (stēˈvĭ-dôrˌ, -dōrˌ) n. One who is employed in the loading or unloading of ships.