Skip to content

Instantly share code, notes, and snippets.

@soareschen
Created September 25, 2017 07:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soareschen/9b63a016174b6123abc073a2be068d48 to your computer and use it in GitHub Desktop.
Save soareschen/9b63a016174b6123abc073a2be068d48 to your computer and use it in GitHub Desktop.
Metaprogramming in JavaScript using proxy and with statement
/*
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())
@hafley66
Copy link

I tried doing this a while back but did not realize you had to check unscopables. Nice!

@Pokute
Copy link

Pokute commented Oct 9, 2019

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