Le 23/02/2011 23:26, David Herman a écrit :
> I've been working on a prototype implementation of the binary data spec in 
> pure JS (implemented via typed arrays) and I've been bitten by the lack of a 
> standard mechanism for subclassing Function.
>
> I'm using proxies for the implementation, and Proxy.createFunction doesn't 
> let me specify a custom prototype. Now, I can understand that this preserves 
> the existing property of the language that the only callable things are 
> either regexps or descendants of Function. But we could extend the proxy API 
> to allow custom functions with user-specified prototypes and still preserve 
> this property:
>
>     Proxy.createFunction(handler, callTrap[, constructTrap[, proto]])
>
> The proto argument would default to the original value of Function.prototype 
> [1]. But if the user provides a prototype, the library could enforce that 
> proto instanceof Function [2]. This way we would preserve the invariant that 
> for any callable value v, either v is a regexp or (typeof v === "function" && 
> v instanceof Function).
With your optional argument, I see a second solution that could be
consistent. The prototype chain could contain the provided prototype
then Function.prototype ("obj --> proto --> Function.prototype -->
null"  as opposed to your proposition which is: "obj --> proto --> null"
). Hence, there would be no need to enforce anything: instanceof would
naturally find Function.prototype in the chain and function proxies
would still be functions.


> Maciej has also suggested a Function.create(...) API for more lightweight 
> creation of function subtypes. This would strengthen the argument for 
> allowing Proxy.createFunction to specify a prototype, since 
> Proxy.createFunction() ought to be able to do anything Function.create() can 
> do.
I'm actually starting to think that this would be a good idea and could
be applied to other things. Here
(http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/)
is a complain of Arrays which cannot be subclassed. A decent solution
for that could be to have a Array.create(proto) method which would
create an array with the prototype chain "myArray --> proto -->
Array.prototype --> null". It could solve all the problems at a time.

> I'm curious to know if there are reasons I've missed why 
> Proxy.createFunction() doesn't support a custom prototype. It seems to me 
> like a nice additional expressiveness without any great loss of language 
> invariants. But I may have missed something.
With your solution, by removing Function.prototype from the chain, proxy
functions couldn't be .call()-ed or .apply()-ed

> Thanks,
> Dave
>
> [1] In the face of multiple globals, this would be the Function associated 
> with the same global as Proxy. (BTW, I intend to work with Andreas on 
> speccing out multiple globals.)
>
> [2] Again, the Function associated with the same global as Proxy.

Regards,

David
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to