Proxy forwarding handlers and accessor properties

David Bruant david.bruant at labri.fr
Sun Jun 19 08:28:29 PDT 2011


Le 19/06/2011 15:53, Tom Van Cutsem a écrit :
> I agree with all of David's points. I wonder though: would it make
> more sense if we'd spec. the "get" trap of Proxy.Handler as:
>
>   get: function(name, proxy) {
>     let val = this.target[name];
>     return typeof val === "function" ? val.bind(this.target) : val;
>   }
>
> Then both method & accessor invocation bind |this| to the target.
>
> It's hard to say what the expected default behavior is for the default
> forwarding handler: should it leave |this| bound to the proxy or to
> the target when forwarding a method invocation? As David mentioned,
> programmers can always implement either, but what should be the
> default? Creating a new bound function per |get| invocation may be
> expensive though. Also, treating a property bound to a function as a
> method is an imperfect heuristic.
And with such a default implementation there would be an object identity
problem:
----
var target = {};
target.a =  function(){};
var f = new ForwardingProxy(target);

f.a !== f.a; // true, because a new bound function is returned each time
----

David


More information about the es-discuss mailing list