Created
December 6, 2015 16:31
-
-
Save MacDada/c0444693b2a3ff6e072b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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