Skip to content

Instantly share code, notes, and snippets.

@louisstow
Last active January 2, 2018 20:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisstow/5610444 to your computer and use it in GitHub Desktop.
Save louisstow/5610444 to your computer and use it in GitHub Desktop.
A simple storage object in JavaScript using localStorage.
var Storage = {
_cache: {},
get: function (key) {
if (!this._cache[key]) {
this._cache[key] = localStorage[key];
}
return this._cache[key];
},
set: function (key, value) {
this._cache[key] = value;
},
sync: function () {
for (var key in this._cache) {
if (this._cache.hasOwnProperty(key)) {
localStorage[key] = this._cache[key];
}
}
}
}
@louisstow
Copy link
Author

Usage

  • Storage.get(key) - Return the saved item by key.
  • Storage.set(key, value) - Saved an item by key.
  • Storage.sync() - Sync all new data to localStorage. Do not use this method during gameplay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment