Skip to content

Documented breaking change for subclassing built-ins. #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2016

Conversation

DanielRosenwasser
Copy link
Member

you may find that:

* methods may be `undefined` on objects returned by constructing these subclasses, so calling `sayHello` will result in ane error.
* `instanceof` will be broken between instances of the subclass and their instances, so `(new FooError()) instanceof FooError` may return `false`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will and not may :D


As part of substituting the value of `this` with the value returned by a `super(...)` call, subclassing `Error`, `Array`, and others may no longer work as expected.
This is due to the fact that constructor functions for `Error`, `Array`, and the like use ES6's `new.target` to adjust the prototype chain;
however, there is no way to emulate the semantics of `new.target` in ECMAScript 5.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be worth noting that it was not working correctelly in TS 2.0 either for the same reason.

Copy link

@rbuckton rbuckton Dec 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with the statement that "there is no way to emulate the semantics of new.target", we just haven't implemented it yet.

Example:

class C {
  constructor() {
    return Object.setPrototypeOf({}, new.target.prototype);
  }
}
//
var C = (function () {
  function C() {
    var _newTarget = this && this instanceof C ? this.constructor : void 0;
    return Object.setPrototypeOf({}, _newTarget.prototype);
  }
  return C;
})();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll amend this so that we state that there is no way to ensure a value of new.target when invoking a constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants