Subclassing native class and instanceof operator

Gray Zhang otakustay at
Tue May 31 06:32:15 UTC 2016

Recently I encountered an issue about subclassing Error and instance
operator, my simplified code is:

class MyError extends Error {
  constructor() {
    super('my error');

let error = new MyError();
console.log(error instanceof MyError);

Surprisingly the output is `false` and `function Error() { [native code] }`

I dived into the ECMAScript 2015 spec and find the behavior is correct,
chapter says:
SuperCall : super Arguments

   1. Let *newTarget* be GetNewTarget
   2. If *newTarget* is *undefined*, throw a *ReferenceError* exception.
   3. Let *func* be GetSuperConstructor
   4. ReturnIfAbrupt
   5. Let *argList* be ArgumentListEvaluation of *Arguments*.
   6. ReturnIfAbrupt
   7. Let *result* be Construct
   (*func*, *argList*, *newTarget*).
   8. ReturnIfAbrupt
   9. Let *thisER* be GetThisEnvironment
   10. Return *thisER*.BindThisValue

Since Error can be called as a function without new operator, the result of
step 7 is an error instance rather than undefined, so this error instance
becomes `this` value of `thisER` and then returned as the result of new
MyError constructor.

The problem is, the spec also said "The Error constructor is designed to be
subclassable." so I think instanceof should work correctly on subclasses,
however it fails now.

This also comes to Map, Set, Array and Object constructors.

Babel 6.7.7 has the correct behavior which fails on instanceof operator but
it really introduces some troubles, is this by design and how could I avoid
such issues?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list