David Bruant david.bruant at
Sat Sep 24 09:03:14 PDT 2011

Le 24/09/2011 15:33, Jake Verbaten a écrit :
> There is no standardized way to create a new function with a prototype
> which is not Function.prototype.
> I propose Function.create
>     /*
>       Creates a new function whose prototype is proto.
>       The function body is the same as the function fbody.
>       The hash of propertydescriptors props is passed to
> defineproperties just like
>       Object.create does.
>     */
>     Function.create = (function() {
>       var functionBody = function _getFunctionBody(f) {
>         return f.toString().replace(/.+\{/, "").replace(/\}$/, "");
>       };
>       var letters = "abcdefghijklmnopqrstuvwxyz".split("");
>       return function _create(proto, fbody, props) {
>         var parameters = letters.slice(0, fbody.length);
>         parameters.push(functionBody(fbody));
>         var f = Function.apply(this, parameters);
>         f.__proto__ = proto;
>         Object.defineProperties(f, props);
>         return f;
>       };
>     })();
> This is the same as Object.create except the second parameter is a
> function.
> It will create a new function whose function body is the same as the
> function passed in.
> I don't believe this is possible to do in ES5 without __proto__
It is not indeed.
This topic has been discussed a couple of times here. There is one
proposal that is more generic than your solution which is the proto
operator [1].

Basically, to create a function with a chosen prototype, you can do:
var f = myProto <| function(a, b){return a+b;}
Object.getPrototypeOf(f); // myProto
f(1, 2); // 3

I'm a big fan of the proto operator proposal, however, as raised
previously this operator relies on the object being created to have an
intialisation syntax. This prevents, for instance, Date objects to have
a custom prototype with this method. I am not very familiar with it yet,
but I think that if they ever came to life, ParallelArrays [2] would
suffer from the same problem.
By the way, could someone add this concern as a note ("open issue" or
"limitation" or something like this) in the proto operator page, please?

One even more generic way which would work even for objects that have no
initialization syntax would to standardize one of [3] or [4]. Is there a
wiki page mentionning these two functions somewhere? If not, may it be
added and linked in some way to the proto operator?



More information about the es-discuss mailing list