I have the Child class that extends Parent class.
So let say I created a new instance "child" out of the Child class.
When I check the condition child instanceof Child
, it returns false.
However, child instanceof Parent
returns true.
Why does this so?
EDIT
So I found this only happens when I extend the Child class with Error class. Let me leave the code example below.
class Child extends Error {
constructor(message) {
super(message);
}
}
const ch = new Child();
console.log(ch instanceof Child);
2nd EDIT
class PullCreditError extends Error {
public name: string;
public message: string;
public attemptsRemaining: number;
constructor(message: string, attemptsRemaining: number) {
super();
Error.captureStackTrace(this, PullCreditError);
this.name = 'PullCreditError';
this.message = message;
this.attemptsRemaining = attemptsRemaining;
}
}
The instanceof operator returns false because a temporary object is created only when a value is read. Because instanceof doesn’t actually read anything, no temporary objects are created, and it tells us the values aren’t instances of primitive wrapper types.
Example 2: Similarly, we can return true or false in a simple JavaScript function. function isEqual (num1, num2) {. if (num1 == num2) {. return true; } else {. return false; } } These were some of the basic implementations of return false statement in JavaScript.
Why does it return false when comparing two similar objects in JavaScript? Suppose we have an example below. JavaScript compares objects and primitives differently. In primitives it compares them by value while in objects it compares them by reference or the address in memory where the variable is stored.
This is defined in the ECMAScript specification Section 7.3.19 Step 3: If Type (O) is not Object, return false. In other word, if the Obj in Obj instanceof Callable is not an object, the instanceof will short-circuit to false directly. Show activity on this post. Show activity on this post.
This is a documented bug:
https://github.com/Microsoft/TypeScript/issues/15875
Extending built-ins like Error, Array, and Map may no longer work
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 ECMAScript 6's new.target to adjust the prototype chain; however, there is no way to ensure a value for new.target when invoking a constructor in ECMAScript 5. Other downlevel compilers generally have the same limitation by default.
The recommendation is to adjust the prototype manually using setPrototypeOf
in the constructor. A fix for your PullCreditError
class would look like this:
export class PullCreditError extends Error {
public name: string;
public message: string;
public attemptsRemaining: number;
constructor(message: string, attemptsRemaining: number) {
super();
Object.setPrototypeOf(this, PullCreditError.prototype); // <-------
Error.captureStackTrace(this, PullCreditError);
this.name = 'PullCreditError';
this.message = message;
this.attemptsRemaining = attemptsRemaining;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With