Created
June 14, 2016 02:27
-
-
Save kallewoof/0fa22a0ab3550e22c50f1357c1d0a4ce 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
const storageHandler = { | |
get(obj, name) { | |
return obj.getItem(name) || obj[name]; | |
}, | |
set(obj, name, value) { | |
obj.setItem(name, value); | |
}, | |
has(obj, name) { | |
return name in obj.d || name in obj; | |
}, | |
deleteProperty(obj, name) { | |
obj.removeItem(name); | |
}, | |
}; | |
class StorageClass { | |
constructor() { | |
this.d = {}; | |
this.k = []; | |
} | |
get length() { | |
return this.k.length; | |
} | |
getItem(name) { | |
return this.d[name]; | |
} | |
setItem(name, value) { | |
if (!this.k.indexOf(name)) { | |
this.k.push(name); | |
} | |
this.d[name] = value; | |
} | |
removeItem(name) { | |
this.k.pop(this.k.indexOf(name)); | |
delete this.d[name]; | |
} | |
clear() { | |
this.d = {}; | |
this.k = []; | |
} | |
} | |
window.localStorage = new Proxy(new StorageClass(), storageHandler); | |
window.sessionStorage = new Proxy(new StorageClass(), storageHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment