Since you have a setter, that gives you enough control over the stores to create your behavior. Since key/values are stored, we can look for last time's values on boot, and upgrade them transparently (if needed). One nice thing about using setObject() and setItem() is that our re-defined properties don't get in the way of re-setting new values. v
I still think this could cause conflicts with other apps that use localStorage, and it reserves the "ƒ" char as special, but if you use your setObject() method each time to save objects, the following works well:
(function() {
function makeDot(key, value){
Object.defineProperty(localStorage, key, {
configurable: true,
enumerable: true,
get: function() {
return typeof value==="string" && value.slice(0,1)==="ƒ" ?
JSON.parse(value.slice(1)) :
value;
}
});
}
Storage.prototype.setObject = function(key, value) {
this.setItem(key, "ƒ"+JSON.stringify(value));
makeDot(key, value);
}
// upgrade existing object stores:
Object.keys(localStorage).forEach(function(a){
var v=localStorage[a];
if(typeof v==="string" && v.slice(0,1)==="ƒ"){
makeDot(a, v);
}
});
}());
var user = {
"name": "testName",
"surname": "testSurname"
};
localStorage.setObject("user", user); //save object
alert( localStorage.user.surname ); // shows: "testSurname"
see http://jsfiddle.net/hrepekbb/ to kick the tires
if you want to set objects with the dot notation, well then then you need to use an observer or maybe proxy reflecting localStorage, but neither of those options are quite ready for prime-time yet.