H5 本地存储 localstorage封装

store.js 是一个实现了浏览器的本地存储的 JavaScript 封装 API,不是通过 Cookie 和 Flash 技术实现,而是使用 localStorage、globalStorage 和 userData 行为。

示例代码:

 1 // 存储 'username' 的值为 'marcus'
 2     store.set('username', '123');
 3 
 4     // 移除 'username' 字段
 5     store.remove('username');
 6 
 7     // 获取 'username'
 8     var aaa = store.get('username');
 9 
10     // 清除所有本地存储
11     store.clear();
12 
13     // 存储对象 - 自动调用 JSON.stringify
14     store.set('user', { name: 'marcus', likes: 'javascript' })
15 
16     // 获取存储的对象 - 自动执行 JSON.parse
17     var user = store.get('user')
18     console.log(user.name + ' likes ' + user.likes)
19 
20     // 从所有存储中获取值
21     store.getAll().user.name == 'marcus'
22 
23     // 遍历所有存储
24     store.forEach(function(key, val) {
25         console.log(key, '==', val)
26     })

GitHub地址:https://github.com/marcuswestin/store.js

原文地址:https://www.cnblogs.com/kongwei/p/6605400.html