Standardizing __proto__

David Bruant david.bruant at
Fri Mar 18 14:36:58 PDT 2011

Le 18/03/2011 18:00, Oliver Hunt a écrit :
> I kind of dislike the proliferation of .create methods.  It's seems inelegant.  What if we had (use __proto__ for semantic description)
> Object.subclass = function(constructor) {
>     "use strict";
>     function cons() {
>         var result = constructor.apply(this, arguments);
>         result.__proto__ = cons.prototype;
>         return result;
>     }
>     cons.prototype = Object.create(constructor.prototype);
>     return cons;
> }
> This would provide a function that does that allows arbitrary "subclassing" of any function.  Obviously there are issues (call vs. construct behaviour, etc), but I think it's not too far off being a decent generic solution.
After giving it further thought, I think that this is a good idea, but
the syntax is wrong.
You'll probably agree that the constructor argument needs a
[[Constructor]] internal method, but your function would need to filter
out things given as arguments which don't. To better target "things that
have a [[Constructor]] internal method", I would add something to
Function.prototype (as it is done with the
Function.prototype.createDelegatingTo idea). So I would go more for
something like:

Function.prototype.subclass = function() {
    function cons() {
        var result = new this; // throws if this doesn't have an
internal [[Construct]]
        result.__proto__ = cons.prototype;
        return result;
    cons.prototype = Object.create(this.prototype);
    return cons.bind(this);

This also addresses the issue you raise "(call vs. construct behaviour)".

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list