[Harmony proxies] Thoughts on function proxies

Tom Van Cutsem tomvc.be at gmail.com
Wed Mar 16 01:11:30 PDT 2011


Hi,

I have no objections to the use cases you're proposing, but in the interest
of keeping the Proxy.createFunction method somewhat simple, why isn't the
following sufficient:

// [[Construct]]-less functions:
var f = Proxy.createFunction(handler, callTrap, function() { throw "can't
construct"; });

// [[Call]]-less functions:
var f = Proxy.createFunction(handler, function() { throw "can't call"; },
constructTrap);

Distinguishing between |null| and |undefined| to trigger one default
behavior or the other feels a bit subtle to me. Are there precedents for
this?

As for the clarifying note on constructors: agreed. It doesn't hurt to be
explicit about this.

Cheers,
Tom

2011/3/15 David Bruant <bruant at enseirb-matmeca.fr>

>  Le 15/03/2011 00:52, David Bruant a écrit :
>
> Hi,
>
> I've been thinking about Function proxy use cases lately. Things that the
> spec do and that could be convenient to emulate as well or just things that
> could be useful.
> * [[Construct]]-less functions.
> All Array.prototype methods are like that as well as all global object
> methods (parseInt...) as well as a most of built-in methods. In some way, if
> you want to define function which wouldn't make sense as constructors (Math
> functions are a caricature of that), [[Construct]]-less functions would be a
> good way to raise error if someone, by accident, write "new blabla()" or
> even "new blabla".
> It has been suggested here:
> https://mail.mozilla.org/pipermail/es-discuss/2011-March/013019.html
> I think that it was a bit too hacky. I would suggest more of a syntax like:
> var pf = Proxy.createFunction({}, myCall, null);
>
> * [[Call]]-less constructors
> Already possible through pf = Proxy.createFunction({}, null,
> myConstructor);
>
> Spoke too quickly. This throws a TypeError both in proposal and FF4
> implementation.
> I would argue that this can be useful if I want to define "just a
> constructor". In most cases, people use function as functions or
> constructor, I have rarely seen both (except for Array or other built-in
> constructors which are, as said, built-in). [[Call]]-less functions would be
> a way to prevent what is intended to be a constructor to be called as a
> function.
>
>
>
> * "new A" behaves exactly like "A". This can be found for Arrays for
> instance.
> Already possible through pf = Proxy.createFunction({}, myCall, undefined);
>
>
> Finally, I'd like to add a note on constructors. On the proposal is
> written: "If no constructTrap is provided, new proxy(...args) is reified as
> calling the proxy’s callTrap with |this| bound to a new object delegating to
> proxy.prototype".
> In order to clarify, I think that instead of "proxy.prototype" it would be
> better to say "the result of calling the get trap on the proxy with argument
> 'prototype'". The get trap is the equivalent to the [[Get]] internal
> method used in ES5.1 13.2.2 step 5.
> Or maybe saying that the [[Construct]] algorithm (13.2.2) will be used.
>
> David
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110316/4dc272ef/attachment.html>


More information about the es-discuss mailing list