一个封装了 localStorage 的对象
Methods
# static get(key) → {String}
获取 localStorage 值
Parameters:
Name | Type | Description |
---|---|---|
key |
String
|
传入存储值的键 key |
返回值
String
Example
localStorage.set('key1','value1');
localStorage.get('key1'); //=> value1
# static isSupport() → {Boolean}
检测当前浏览器是否支持 localStorage 存储
返回当前浏览器是否支持 localStorage 存储
Boolean
Example
// 在支持 localStorage 的浏览器中
localStorage.isSupport() //=> true
# static parse(key) → {Object}
获取 localStorage 值并且通过 JSON.parse 解析为 JS 对象
如果无法成功解析,则返回字符串值
Parameters:
Name | Type | Description |
---|---|---|
key |
String
|
传入存储值的键 key |
返回值
Object
Example
localStorage.set('key2',JSON.stringify({a:1}));
localStorage.parse('key2'); //=> {a:1}
# static remove(key)
删除 localStorage 键值
Parameters:
Name | Type | Description |
---|---|---|
key |
*
|
传入存储值的键 key |
Example
localStorage.remove('key2');
localStorage.get('key2') //=> null
# static set(key, value)
设置 localStorage 键值
Parameters:
Name | Type | Description |
---|---|---|
key |
String
|
传入存储值的键 key |
value |
String
|
传入存储值的值 value |
Example
localStorage.set('key1','value1');
localStorage.get('key1'); //=> value1