Skip to content

Instantly share code, notes, and snippets.

@liuwenzhuang
Created March 6, 2019 09:08
Show Gist options
  • Save liuwenzhuang/6152772672a1dd3067f7ee8458408338 to your computer and use it in GitHub Desktop.
Save liuwenzhuang/6152772672a1dd3067f7ee8458408338 to your computer and use it in GitHub Desktop.
Add set/get object capacity to localStorage 为localStorage增加读、写Object的能力
if (typeof Storage === 'undefined') {
window.localStorage = {};
localStorage.setItem = function (key, val) {
this[key] = val;
};
localStorage.getItem = function (key) {
return this[key];
};
window.localStorage.setObject = function (key, value) {
this[key] = value;
};
window.localStorage.getObject = function (key) {
return this[key];
};
} else {
Storage.prototype.setObject = function (key, value) {
this.setItem(key, JSON.stringify(value));
};
Storage.prototype.getObject = function (key) {
var value = this.getItem(key);
return value && JSON.parse(value);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment