Objects, Arrays, and Local Storage

  • 8/19/2011

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) {};
  }
}

Hey, I'm RJ! For more learnings about software and management, find me @rjzaworski or sign up for my semi-regular newsletter.

Let’s keep in touch

Send me timely updates on software, product, and process.