Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend an ES6 class with ES5? [duplicate]

The reasons for doing this are complicated, but it boils down to flow not understanding mixins or any other way of modifying an ES6 class's prototype. So I'm falling back to ES5, but I can't figure out how to call the constructor of an ES6 class without new:

class A {
  constructor() {}
}

function B() {
  // what do I put here?  I would do something like
  // A.prototype.constructor.call(this) but that throws an error saying the
  // constructor can only be called with `new`
}
B.prototype = Object.create(A.prototype);
like image 792
Nobody Avatar asked Jun 19 '18 12:06

Nobody


1 Answers

Answering this myself:

class A {
  constructor() {}
}

function B() {
  Object.assign(this, new A());
}
B.prototype = Object.create(A.prototype);

Not sure if there are any side-effects here or not

like image 153
Nobody Avatar answered Oct 05 '22 23:10

Nobody