Skip to content

Instantly share code, notes, and snippets.

@louisstow
Last active Jan 2, 2018
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
Copy link
Author

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