Hacker News new | ask | show | jobs
by jasonmcaffee 2499 days ago
Has anyone tried running the solution? It doesn't seem to work... The below code results in the console logging the document object, which has the document object, and the code hits a ReferenceError when trying to log the 'a' variable. Calls to p.whateverPropYouMakeUp result in the log 'get for target: ...'

    const proxyHandler = {
      get(target, name){
        console.log(`get for target: `, target, name);
        return 'tacos';
      },
    }

    const p = new Proxy({}, proxyHandler);

    with (p){
      console.log(`document with proxy: `, document);
      console.log(`access random property: `, a);
    }
https://jsfiddle.net/wf02n4gs/
1 comments

I was able to make it work by adding a has() trap.

https://jsfiddle.net/wepmLrh0/

Thanks - I extended that to show two "widgets" being fed 2 different JQueries: https://jsfiddle.net/habla/a56807ng/
Awesome! Thanks!