Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active May 6, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/2616f1fb1f1cdb140784b5bb64d6d8d1 to your computer and use it in GitHub Desktop.
Save JadenGeller/2616f1fb1f1cdb140784b5bb64d6d8d1 to your computer and use it in GitHub Desktop.
Shadowing Dictionary
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