Sub topic 15
When instanceof Breaks: ES5 Transpilation
This chapter assumes you know how to subclass Error with class extends, how prototype chains decide instanceof, and what a transpiler such as TypeScript or Babel does. The earlier chapters relied on instanceof to tell errors apart. That only works if each error instance really has your class's prototype. Some build setups break this without any warning.
What goes wrong
If you compile to an ES5 target, the compiler has to rewrite class into constructor functions. The usual output calls the parent constructor as a plain function: Error.call(this, message). Most constructors called this way set up this. Error does not. Called as a function, Error ignores this and returns a brand-new Error object. The compiled code then uses that returned object as the instance, so the instance's prototype is Error.prototype and your subclass's prototype never appears in its chain.
You can reproduce this in plain JavaScript by writing what TypeScript emits for target: "ES5":
function LegacyError(message) {
var _this = Error.call(this, message) || this;
_this.name = 'LegacyError';
return _this;
}
LegacyError.prototype = Object.create(Error.prototype, {
constructor: { value: LegacyError, writable: true, configurable: true },
});
var err = new LegacyError('boom');
console.log(err instanceof LegacyError); // false
console.log(err instanceof Error); // true
console.log(err.name); // 'LegacyError'
console.log(typeof err.toJSON); // 'undefined' even if you add LegacyError.prototype.toJSONBecause name is an own property set on the object, it looks correct. That makes the bug easy to miss. Your instanceof branches fall through to the generic handler, and any methods you define on the class are missing.
Fixes
Option 1: Target ES2015 or later
Every current browser and every supported Node.js version runs native classes. If you don't need to support ES5-only environments, raise the target. With native classes the compiler emits the class as written and the engine builds the prototype chain correctly.
{
"compilerOptions": {
"target": "ES2015"
}
}Babel 7 also wraps built-in parents such as Error with a helper that uses Reflect.construct, so the default class transform usually works. The helper can't do this in runtimes that lack Reflect.construct.
Option 2: Restore the prototype manually
If you must ship ES5, reset the prototype in the constructor:
class LegacyError extends Error {
constructor(message, options) {
super(message, options);
Object.setPrototypeOf(this, new.target.prototype);
this.name = 'LegacyError';
}
}Use new.target.prototype rather than LegacyError.prototype. That way a subclass of LegacyError gets its own prototype too, and you only need the fix in the base class of your hierarchy. TypeScript compiles new.target down for ES5 targets.
Exercise
In the ES5-style example, err.name prints 'LegacyError' but err instanceof LegacyError is false. Explain both results.
Show answer
The constructor sets name as an own property on the object returned by Error.call, so name reads correctly. That object was created by Error itself, so its prototype is Error.prototype. LegacyError.prototype is not in its chain, which makes instanceof LegacyError return false.
Exercise
Why use new.target.prototype instead of LegacyError.prototype in the fix?
Show answer
new.target refers to the class that was actually called with new. If a subclass inherits from LegacyError, the instance gets the subclass's prototype, so instanceof checks against the subclass still work. Hard-coding LegacyError.prototype would set every subclass instance's prototype to LegacyError.prototype.