plugin LsyStorage
author loushengyue
website http://www.loushengyue.com
version 1.0.2
methods
.getItem(key[string])
.getItemsByKeys(keys[array])
.getArr(prex[string])
.setItem(key[string],value[string,object])
.setArr(prex[string],values[array])
.setList(keys[array],values[array])
.removeItem(key[string])
.clearAll()
由于使用localStorage所提供的setItem()
、getItem()
、removeItem()
等方法具有局限性(对数组,对象存取不方便),在此,封装了一个插件LsyStorage.js,完善了对数组,对象的读取方法。
LsyStorage.js下载:点击下载
$ bower install https://github.com/loushengyue/LsyStorage.git
例如:
var arr = ['apple', 'banana', 'orange', 'grape'];
var keys = ['fruits_0', 'fruits_3'];
LsySession.setArr('fruits', arr);
var list = LsySession.getItemsByKeys(keys);
console.log(list);//['apple','grape']
例如:
var key = 'zhangsan';
var value = 'He is 20 years old boy.';
var objKey = 'lisi';
var objVal = {
age: 18,
sex:'man'
};
LsyStorage.setItem(key, value); //存储字符串
LsyStorage.setItem(objKey, objVal); //存储对象
例如:
var prex = 'fruit';
var fruits = ['apple', 'banana', 'orange'];
LsyStorage.setArr(prex, fruits); // 以变量prex为前缀存储数组fruits
例如:
var keys = ['aa', 'bb', 'cc'];
var values = ['the value of aa', 'the value of bb', 'the value of cc'];
LsyStorage.setList(keys, values); //以keys为键,以vualue为值进行map映射法存储,注意两个数组的长度必须一致
例如:
var key = 'zhangsan';
var student = LsyStorage.getItem(key); //通过键key获取localStorage所对应的value值
console.log(student); // He is 20 years old boy.
例如:
var prex = 'fruit';
var fruits = LsyStorage.getArr(prex); //通过键前缀prex获取localStorage所对应的系列value值
console.log(fruits); //['apple', 'banana', 'orange']
例如:
var key = 'lisi';
LsyStorage.removeItem(key); //通过键key删除localStorage所对应的value值;
var lisi = LsyStorage.getItem(key);
console.log(lisi); //undefined
例如:
LsyStorage.clearAll(); // 删除所有的localStorage信息