[[Invoke]] and implicit method calls

Tom Van Cutsem tomvc.be at gmail.com
Tue Sep 24 01:23:01 PDT 2013


2013/9/23 Allen Wirfs-Brock <allen at wirfs-brock.com>

>
> On Sep 23, 2013, at 1:29 AM, Tom Van Cutsem wrote:
>
> I'm willing to consider changing the behavior of ForwardingHandler.get to
> auto-wrap functions and re-bind their this-value. I think you may be right
> that for users subclassing ForwardingHandler, they probably want the
> this-rebinding to happen for them by default.
>
>
> I think you should try to define it since you are arguing that such
> warppering is the solution.  However, as I described above, I don't think
> you can define a sufficiently generic wrappering semantics.
>

You are right. I accepted your challenge and started thinking about how to
wrap the method in the "get" trap.

The most trivial solution is to just wrap every target function in a bound
function, like so:

get: function(target, name, receiver) {
  var prop = target[name];
  if (typeof prop === "function") { return prop.bind(target); }
  return prop;
}

However, functions are objects so may have properties. The bound function
would not expose the properties of its wrapped function. Gratuitous
wrapping also breaks identity if code depends on the identity of a
function. And the direct proxy would even throw a TypeError if the target's
property is non-configurable non-writable.

To solve all of that, you'd need proper membranes. But using membranes to
fix the this-rebinding problem seems like total overkill.

So forget ForwardingHandler.get doing auto-wrapping. The generic solution
is membranes. Anything else needs application-specific consideration.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130924/9d50a16b/attachment-0001.html>


More information about the es-discuss mailing list