Adding a non-class-syntax method for subclassing native objects

Renki Ivanko fatalis.erratum at gmail.com
Tue May 3 23:21:09 UTC 2016


It's currently impossible to inherit the behavior of exotic builtin objects
like `Array` without using the `class extends` syntax, which is surprising
to someone who has only seen the `class` syntax described as syntactical
sugar for prototypal inheritance, since it means that the `class extends`
syntax can't be fully desugared to the non-`class` syntax. It means that
describing the `class` syntax as syntactical sugar should be qualified to
be fully accurate (since trying to desugar it with, for example, Babel can
potentially break things even in browsers that otherwise support native
subclassing), and that non-`class` syntax is a second-class citizen in the
language. Adding a method for using the native subclassing without the
`class extends` syntax would be consistent with ES5 adding
`Object.create()` that allows using prototypal inheritance without the
`new` operator.

The new method could be called `Object.inherits()`; a simplified example of
how it could be used:

```class Foo extends Array() {}

// ...desugars to

function Foo() {
    function Foo(...args) {
        return Object.getPrototypeOf(Foo)(...args)
    }

    Object.inherits(Foo, Array)

    return Foo
}

Object.inherits = function(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype, {
        constructor: {value: subClass},
        [Symbol.species]: {value: subClass},
    })
    Object.setPrototypeOf(subClass, superClass)
    // make subClass imbue the objects it constructs with native exotic
behavior here
}```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160504/dd3efda9/attachment.html>


More information about the es-discuss mailing list