Last active
May 6, 2017 23:46
-
-
Save JadenGeller/2616f1fb1f1cdb140784b5bb64d6d8d1 to your computer and use it in GitHub Desktop.
Shadowing Dictionary
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
function Shadow() { | |
return new Proxy({}, { | |
get: function (target, name) { | |
if (name in target) { | |
let stack = target[name]; | |
return stack[stack.length - 1]; | |
} else { | |
return undefined; | |
} | |
}, | |
has: function (target, name) { | |
return (name in target); | |
}, | |
set: function (target, name, value) { | |
if (name in target) { | |
target[name].push(value); | |
} else { | |
target[name] = [value]; | |
} | |
return true; | |
}, | |
deleteProperty: function (target, name) { | |
if (!(name in target)) { | |
return false; | |
} | |
if (target[name].length > 1) { | |
target[name].pop(); | |
} else { | |
delete target[name]; | |
} | |
return true; | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment