Objects, Arrays, and Local Storage
- 8/19/2011
- ·
- #index
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) {};
}
}```