Created
September 25, 2017 07:49
-
-
Save soareschen/9b63a016174b6123abc073a2be068d48 to your computer and use it in GitHub Desktop.
Metaprogramming in JavaScript using proxy and with statement
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
/* | |
This code snippet demonstrates how to do metaprogramming | |
in JavaScript using proxy and with statement. | |
Run this in non-strict mode. | |
*/ | |
const proxy = new Proxy({}, { | |
get(target, key) { | |
if(key === Symbol.unscopables) return {} | |
return `${key.toUpperCase()} ` | |
}, | |
has(target, key) { | |
return true | |
} | |
}) | |
const main = () => { | |
with (proxy) { | |
return This + is + Black + Magic | |
} | |
} | |
console.log(main()) |
I'm tempted to press the "Report abuse" link for what you've done to JavaScript. Great work!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried doing this a while back but did not realize you had to check unscopables. Nice!