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.
Cheers,
Tom
2011/6/17 David Bruant david.bru...@labri.fr
Le 17/06/2011 08:42, David Flanagan a écrit :
On 6/16/11 5:20 PM, David Bruant wrote:
Le 17/06/2011 01:54, David Flanagan a écrit :
// When we create a proxy with a forwarding handler, though, the this
value
// is different in the two cases.
var handler = {
target: o,
get: function(receiver, name) {
return this.target[name]; // Same as
Proxy.Handler.prototype.get
}
}
If you get rid of the get trap (you need a getOwnPropertyDescriptor
trap though), the default get (derived) trap will be called instead and
basically do what you do afterward (call to getOwnPropertyDescriptor
trap which will call Object.**getOwnPropertyDescriptor(this.**target,
name)+d.get.call(receiver)). The 'this' binding is performed by the
engine (with the receiver as value) after the property descriptor has
been returned (as explained in strawman:proxy_drop_receiver).
Thanks. I didn't realize that the default get behavior was to do what I
put in my second handler.
Yep. http://wiki.ecmascript.org/**doku.php?id=harmony:proxies#**
trap_defaultshttp://wiki.ecmascript.org/doku.php?id=harmony:proxies#trap_defaults
Besides type checking, the code is the same than yours for the get trap.
In a way, it's reassuring that people experimenting like you do reach the
same solution to the same problem independently.
Still, my basic question remains, though: is it a bug that a forwarding
proxy created with Proxy.Handler behaves differently in this way (this value
in methods is different than the this value for accessors) than the object
to which it forwards?
I do not think it's a bug. What happens with your code is coherent with
ECMAScript (and apparently, the JS implementation is consistent with that
too).
I think that a more important question is: can you achieve what you want
(giving the impression that this is the same for both getters/setters and
methods) with the current proxy API?. If so, is it natural or is it a
hack?
Apparently, you can achieve what you want and my opinion is that the
solution is natural (using the derived trap default behavior). Removing a
trap to achieve your goal may sound not natural, but strawman:derived_traps_
**forwarding_handler seems to be a decent solution for that.
(Originally I had a second question about the proposed removal of the
receiver argument, but now I understand that that strawman is contingent on
the addition of a proxy argument to the handler methods, so let's disregard
that second question...)
Yes, I'm sorry, I should have mentionned that. I tend to consider this
strawman as already accepted since it sounds so natural to me and has found
several justifications on es-discuss. It is however true it still needs to
be accepted (July TC-39 meeting, hopefully?) and SpiderMonkey will need to
be adjusted consequently.
David
__**_
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/**listinfo/es-discusshttps://mail.mozilla.org/listinfo/es-discuss
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss