How to modify the scope chain without `with` ?

Coroutines coroutines at gmail.com
Mon Feb 15 17:50:01 UTC 2016


On Mon, Feb 15, 2016 at 1:43 AM, Andreas Rossberg <rossberg at google.com> wrote:

> Without wanting to say much on the overall viability of your plan, but
> proxies do work with `with`. However, your code has at least two bugs:
>
> (1) It's not defining a custom `has` trap. That is needed for `with`,
> otherwise it will just check the target object, which has no `f`.
>
> (2) You cannot return `console.log` first-class and expect it to work (at
> least not on all browsers). Known JavaScript issue.
>
> This fixed version works fine on V8 4.9 / Chrome 49:
>
> function f() { console.log("failure") }
>
> let p = new Proxy({}, {
>   has() { return true },
>   get() { return x => console.log(x) }
> });
>
> with (p) { f("success") };
>
> /Andreas

Thank you very, very much!  I feel embarrassed I missed that it needed
a has(), but I am glad I have a solution that works now - I thought it
impossible!


More information about the es-discuss mailing list