Skip to content

Instantly share code, notes, and snippets.

@MacDada
Created December 6, 2015 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MacDada/c0444693b2a3ff6e072b to your computer and use it in GitHub Desktop.
Save MacDada/c0444693b2a3ff6e072b to your computer and use it in GitHub Desktop.
/**
* should behave like LocalStorage
*/
var inMemoryStorage = (function () {
var items = {};
return {
length: 0, // todo: read only
getItem: function (key) {
return 'undefined' !== typeof items[key] ? items[key] : null;
},
setItem: function (key, value) {
if ('undefined' === typeof items[key]) {
// todo: handling values other than string
items[key] = value;
this.length++;
}
},
deleteItem: function (key) {
if ('undefined' !== typeof items[key]) {
delete items[key];
this.length--;
}
},
key: function (index) {
// todo
},
clear: function () {
items = {};
this.length = 0;
}
};
})();
inMemoryStorage.setItem('foo', 'bar'); // it works
inMemoryStorage['foo'] = 'bar'; // how do I get it to work?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment