Objects, Arrays, and Local Storage

HTML5′s local storage system is an incredibly useful feature, but I’m not the biggest fan of having to serialize Objects and Arrays every time they need saving. So I’ve taken to including getter and setter methods in my storage pattern to do the heavy lifting. To wit:

window.storage = {
  store:localStorage,
  get: function( key ) {
    try {
      return JSON.parse(this.store[key]);
    } catch(e) {};
    return undefined;
  },
  set: function( key, value) {
    try {
      this.store[key] = JSON.stringify(value);
    } catch(e) {};
  }
}

Let’s keep in touch

Get noise-free updates on software, product, and process.

Hey, I'm RJ: digital entomologist and intermittent micropoet, writing from the beautiful Rose City.