Created
August 20, 2015 06:33
-
-
Save dvdbng/17d9b05fddd5ee4d9aa5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;(function(){ | |
function makeStorage(){ | |
var keys = []; | |
var storage = Object.create(Object, { | |
getItem: {value: function(k) { | |
if(this.hasOwnProperty(k)) { | |
return this[k]; | |
} | |
}}, | |
setItem: {value: function(k, v) { | |
this[k] = String(v); | |
update(); | |
}}, | |
removeItem: {value: function(k) { | |
if(this.hasOwnProperty(k)) { | |
delete this[k]; | |
} | |
update(); | |
}}, | |
clear: {value: function(){ | |
for(var k in this){ | |
if(this.hasOwnProperty(k)) { | |
delete this[k]; | |
} | |
} | |
keys.splice(0, keys.length); | |
}}, | |
key: {value: function(i) { | |
return keys[i]; | |
}}, | |
length: {get: function(){ | |
return keys.length; | |
}} | |
}); | |
var update = function() { | |
keys.splice(0, keys.length); | |
for(var k in storage){ | |
if(storage.hasOwnProperty(k)){ | |
keys.push(k); | |
} | |
} | |
}; | |
return { | |
storage: storage, | |
update: update | |
}; | |
} | |
var local = makeStorage(); | |
var session = makeStorage(); | |
window.__defineGetter__('localStorage', function(){ | |
local.update(); | |
return local.storage; | |
}); | |
window.__defineGetter__('sessionStorage', function(){ | |
session.update(); | |
return session.storage; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment