Created
March 6, 2019 09:08
-
-
Save liuwenzhuang/6152772672a1dd3067f7ee8458408338 to your computer and use it in GitHub Desktop.
Add set/get object capacity to localStorage 为localStorage增加读、写Object的能力
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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