扫一扫分享
一种功能丰富且友好的方式,可以利用localStorage和sessionStorage(JSON,命名空间,扩展等)。
主要的存储功能可以处理set,get,transact,setAll,getAll和clear 行为直接。这些被称为:
store(key, data); // sets stringified data under key
store(key); // gets and parses data stored under key
store(key, fn[, alt]); // run transaction function on/with data stored under key
store({key: data, key2: data2}); // sets all key/data pairs in the object
store(); // gets all stored key/data pairs as an object
store((key, data)=>{ }); // calls function for each key/data in storage, return false to exit
store(false); // clears all items from storage
[方括号]中的参数是可选的。还有更明确和多功能的功能:
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([fillObj]); // === store();
store.transact(key, fn[, alt]); // === store(key, fn[, alt]);
store.clear(); // === store(false);
store.has(key); // returns true or false
store.remove(key[, alt]); // removes key and its data, then returns the data or alt, if none
store.each(fn[, fill]); // === store(fn); optional call arg will be 3rd fn arg (e.g. for gathering values)
store.add(key, data); // concats, merges, or adds new value into existing one
store.keys([fillList]); // returns array of keys
store.size(); // number of keys, not length of data
store.clearAll(); // clears *ALL* areas (but still namespace sensitive)
手机预览