extending an ES6 class using ES5 syntax?

Domenic Denicola d at domenic.me
Sat May 14 01:04:22 UTC 2016

I believe this will work in most cases:

function B() {
  const obj = new A();
  Object.setPrototypeOf(obj, new.target.prototype); // or B.prototype, but if you derive from B you'll have to do this dance again

  // use obj instead of this

  return obj;

Also, in general you should do

Object.setPrototypeOf(B.prototype, A.prototype);
Object.setPrototypeOf(B, A);

instead of

B.prototype = Object.create(A.prototype);

for slightly better semantics, including class-side inheritance and not clobbering `.constructor`.

More information about the es-discuss mailing list