extending an ES6 class using ES5 syntax?

John Lenz concavelenz at gmail.com
Sat May 14 05:22:57 UTC 2016


I'm trying to provide a path where common code can migrate to ES6 classes
before all the consumers have.   So I was really looking for something that
didn't require the subclasses to be touched at all (I wasn't clear about
this).  I have a fair amount of control over how the inheritance is setup
and how the ES6 class is written but that is about it.

As-is, it seems every subclass constructor in the hierarchy must be changed
to not use "this" and that means all the leaves must be changed first and
they might as well migrate to ES6 classes which I'm trying not to wait on.

It seems I need some way to opt-out of "constructor method can't be called
without new".

I'm pondering a half-way solution of  parallel set of constructor functions
and hoping every subclass uses the "super call" abstractions we provide
(which I know isn't true) so I can redirect the "constructor" call:

class SomeSuper {
  constructor() {
    super();
    init();
  }

  es5constructor() {
    super.es5construcor(...);
    init();
  }

  init() {
     ...
  }
}


This makes me very unhappy and I would rather make the ES6 class a little
less safe and get more code moving over to ES6 classes.

As is, folks are talking about transpiling "forever" because they can avoid
this problem.






On Fri, May 13, 2016 at 7:51 PM, Rob Brackett <rob at robbrackett.com> wrote:

> If it's helpful, you can look at how newless handles this:
> https://github.com/mr0grog/newless/
>
> It's a little more complicated than the earlier replies because many
> implementations have classes but not `Reflect.construct`, but it goes
> gracefully allow ES5- function-style constructors to inherit from
> class-style constructors.
>
> At the end of the day, the one restriction you can't really work around is
> not being able to touch `this` inside the constructor before calling the
> super class’s constructor (unless you don't call it at all, though that
> will no longer work if/when you transition to proper classes).
>
> -Rob
>
>
> On May 13, 2016, at 6:35 PM, Renki Ivanko <fatalis.erratum at gmail.com>
> wrote:
>
> That said, can't the right thing be done using Reflect.construct?
>>
>
> It can:
>
> ```js
> function B() {
> return Reflect.construct(A, arguments)
> }
> ```
>
> `instanceof B` will be `false`, though.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160513/37c65bad/attachment.html>


More information about the es-discuss mailing list