custom proto arg for Proxy.createFunction?

David Herman dherman at
Wed Feb 23 14:26:58 PST 2011

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).

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 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.


[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.

More information about the es-discuss mailing list