Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@louisstow

louisstow May 20, 2013

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.
Owner

louisstow commented May 20, 2013

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