Allowing super to use EvaluateConstruct, or "How do I inherit from Date?"

Erik Arvidsson erik.arvidsson at gmail.com
Mon Jan 7 08:35:28 PST 2013


On Sun, Jan 6, 2013 at 10:32 PM, Brandon Benvie
<brandon at brandonbenvie.com> wrote:
> Despite separating out @@create into a property of functions, which allows
> allocation and setting of BuiltinBrand for subclasses, this still leaves
> inheriting from most builtins just short of possible because they are
> "construct sensitive". The new ES6 classes like Map, Set, WeakMap, and typed
> arrays have been carefully designed so the constructor initializes `this`.
> But this still leaves most of the existing builtins un-subclassable.

I think you are making this sound worse than it is.

What isn't working is that super in the constructor doesn't do what you
expect. The following should work

class MyArray extends Array {
  constructor(...args) {
    // Intentionally not doing the crazy one-arg-is-length-if-uint32
    this.push(...args);
  }
}

class MyDate extends Date {
  constructor(..args) {
    var temp = new Date(...args);
    this.setTime(temp.getTime());
  }
}

I agree that it will be confusing to most users and that we should try
to make super in a constructor work for these cases.

--
erik


More information about the es-discuss mailing list