2019-07-12 admin


什么是storejs,Local storage localstorage package provides a simple API


JavaScript localStorage

Build Status Coverage Status GitHub issues GitHub forks GitHub stars store.js

A simple, lightweight JavaScript API for handling browser localStorage , it is easy to pick up and use, has a reasonable footprint 2.36kb(gzipped: 1.04kb), and has no dependencies. It should not interfere with any JavaScript libraries or frameworks.


<g-emoji class=“g-emoji” alias=“rocket” fallback-src=“”>🚀</g-emoji> Has no dependencies <g-emoji class=“g-emoji” alias=“seedling” fallback-src=“”>🌱</g-emoji> Works in all browsers <g-emoji class=“g-emoji” alias=“fire” fallback-src=“”>🔥</g-emoji> Heavily tested <g-emoji class=“g-emoji” alias=“package” fallback-src=“”>📦</g-emoji> Supports AMD/CommonJS <g-emoji class=“g-emoji” alias=“boom” fallback-src=“”>💥</g-emoji> store.min.js 2.36kb(gzipped: 1.04kb)


Installed via npm. You will need Node.js installed on your system.

$ npm install storejs --save
import store from 'storejs';

store('test', 'tank', 1)

Or manually download and link storejs in your HTML, It can also be downloaded via UNPKG or jsDelivr CDN:

<script src=""></script>
<script type="text/javascript">
  store('test', 'tank');

Basic Usage

store(key, data);                 // Single storage string data
store({key: data, key2: data2});  // Bulk storage of multiple string data
store(key);             // Get `key` string data
store("?key");          // Determine if the `key` exists
store();                // Get all key/data
//store(false);🔫       // (Deprecated) because it is easy to empty the storage because of a null value or an error
//store(key, false); 🔫  // (Deprecated)

store.set(key, data[, overwrite]);    // === store(key, data);
store.set({key: data, key2: data2})   // === store({key: data, key2: data});
store.get(key[, alt]);                // === store(key);
store.get("?key");                    // Determine if the `key` exists
store.get("key1", "key2", "key3");    // Get `key1`,`key2`,`key3` data
store.remove(key);                    // ===store(key,false)
store.clear();                      // Clean all key/data
store.keys();                       // Returns an array of all the keys
store.forEach(callback);            // Loop traversal, return false to end traversal;               // Search method

store.has(key); //⇒ Determine if there is a return true/false

//⇒ Provide callback method to process data
store('test', (key,val) => {
  console.log(val) // Processing the data obtained through the test here
  return [3,4,5] // Return data and set store

store(['key', 'key2'], (key) => {
  // Get data processing of multiple keys, return and save;
  console.log('key:', key)
  return '逐个更改数据'

Storage Event

Responding to storage changes with the StorageEvent

}else if(window.attachEvent){
function handle_storage(e){
Property Type Description
key String The named key that was added, removed, or moddified
oldValue Any The previous value(now overwritten), or null if a new item was added
newValue Any The new value, or null if an item was added
url/uri String The page that called the method that triggered this change

Chained Call

store.set('ad', 234).get('ad')


  • [ ] store.get([key,key2]) Get method, return json
  • [ ] store([key,key2]) Get method, return json
  • [ ] onStorage Method test cases, and implementation


Licensed under the MIT License.


转载请注明:文章转载自 JavaScript中文网 []