Posted By

zfisic on 11/30/12


Tagged

localstorage prototypes


Versions (?)

Augment Storage Prototype with getObject, setObject


 / Published in: JavaScript
 

Provides getter/setter functionality for objects in local storage. Utilizes JSON.stringify and JSON.parse.

  1. if (window.Storage) {
  2.  
  3. Storage.prototype.setObject = function(key, value) {
  4. this.setItem(key, JSON.stringify(value));
  5. }
  6.  
  7. Storage.prototype.getObject = function(key) {
  8. try {
  9. return JSON.parse(this.getItem(key));
  10. }
  11. catch(e) {
  12. return null;
  13. }
  14. }
  15.  
  16. }

Report this snippet  

You need to login to post a comment.