store.js本地存儲 APIs
本地存儲 APIs
store.js 是一個(gè)實(shí)現(xiàn)了瀏覽器的本地存儲的 JavaScript 封裝 API,不是通過 Cookie 和 Flash 技術(shù)實(shí)現(xiàn),而是使用 localStorage。小弟我主要是用于chrome,Safari,手機(jī)Web等先進(jìn)瀏覽器里面跑。so....大家可以做兼容哦....
store(key, data); //單個(gè)存儲字符串?dāng)?shù)據(jù)
store({key: data, key2: data2}); //批量存儲多個(gè)字符串?dāng)?shù)據(jù)
store(key); //獲取key的字符串?dāng)?shù)據(jù)
store(); //獲取所有key/data
store(false); //清空所有key/data
store.set(key, data[, overwrite]);//=== store(key, data);
store.setAll(data[, overwrite]); //=== store({key: data, key2: data});
store.get(key[, alt]); //=== store(key);
store.getAll(); //=== store();
store.clear(); //=== store(false);
store.keys(); //返回所有key的數(shù)組
store.forEach(callback); //循環(huán)遍歷,返回false結(jié)束遍歷
store.has(key); //?判斷是否存在返回true/false
store.remove(key); //?刪除key包括key的字符串?dāng)?shù)據(jù)
set
單個(gè)存儲或刪除字符串?dāng)?shù)據(jù)
store.set(key, data[, overwrite]);
效果相同store(key, data);
store.set("wcj","1") //? 1
store.set("wcj") //? 刪除wcj及字符串?dāng)?shù)據(jù)
setAll
批量存儲多個(gè)字符串?dāng)?shù)據(jù)
store.setAll(data[, overwrite])
效果相同store({key: data, key2: data});
store.setAll({
"wcj1":123,
"wcj2":345
}) //存儲兩條字符串?dāng)?shù)據(jù)
store.setAll(["w1","w2","w3"])
//存儲三條字符串?dāng)?shù)據(jù)
// 0? "w1"
// 1? "w2"
// 2? "w3"
獲取key的字符串?dāng)?shù)據(jù)
store.get(key[, alt])
效果相同store(key)
store.get("wcj1") //獲取wcj1的字符串?dāng)?shù)據(jù)
store("wcj1") //功能同上
getAll
獲取所有key/data
store.getAll()
效果相同store()
store.getAll() //?JSON store() //功能同上
clear
清空所有key/data
store.clear()
效果相同store(false)
store.clear() // store(false) //功能同上
keys
返回所有key的數(shù)組
store.clear()
store.keys() //?["w1", "w2", "w3"]
has
判斷是否存在返回true/false
store.has(key)
store.has("w1"); //?true
remove
刪除key包括key的字符串?dāng)?shù)據(jù)
store.remove(key)
store.has("w1"); //刪除w1
store.set("w1") //這樣也是 刪除w1
forEach
循環(huán)遍歷,返回false結(jié)束遍歷
store.forEach(function(k,d){
console.log(k,d)
if (k== 3) return false
})評論
圖片
表情
