A strawman for subclassing

David Bruant david.bruant at labri.fr
Mon Jun 13 15:46:48 PDT 2011


Hi,

The subclassing native constructor issue came out several times on the
list and on the web. However, I do not see a strawman for this. There
are strawman:array_create and strawman:array_subtypes, but no mention of
a generic solution.

>From the "standardizing __proto__" thread [1], I retained two
interesting generic solutions:
1) Olivier Hunt at
https://mail.mozilla.org/pipermail/es-discuss/2011-March/013141.html
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;
}

2) Mike Shaver at
https://mail.mozilla.org/pipermail/es-discuss/2011-March/013154.html
Function.prototype.createDelegatingTo = function (obj)
{
  var o = new this; // really new-with-apply(arguments) here
   o.__proto__ = obj;
   return o;
}

Could one of these (or another solution if these do not meet some
expectations) be in a strawman and have a chance to ship on ES.next?
With Maps and Sets, people will want to extend the prototype of these
and come across the exact same issues than usual when it comes to
extending the prototype. Having a native and standard way to subclass
them would be beneficial for all in my opinion.

Thanks,

David

[1] https://mail.mozilla.org/pipermail/es-discuss/2011-March/013131.html


More information about the es-discuss mailing list