compat-table icon indicating copy to clipboard operation
compat-table copied to clipboard

Test for super() returning object other than `this`

Open justinfagnani opened this issue 8 years ago • 1 comments

If a constructor returns a value other than this, then in subclasses super() calls to that constructor should use the result as this in the subclass constructor.

This is specified in 12.3.5.1, step 10 of SuperCall: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-super-keyword

Here's an example that works in correct ES2015 implementation, and fails in some others:

class Foo {
  constructor() {
    return {
      x: 1,
    };
  }
}

class Bar extends Foo {

  constructor() {
    super();
    this.y = 2;
  }
}

let o = new Bar();
console.assert(o.x === 1 && y === 2);

justinfagnani avatar Mar 18 '16 21:03 justinfagnani

console.assert(o.x === 1 && o.y === 2);

nilssolanki avatar Mar 23 '16 16:03 nilssolanki