为什么 Javascript 中的子对象的 instanceof returns false
Why instanceof returns false for a child object in Javascript
我有扩展父 class 的子 class。
假设我从子 class 中创建了一个新实例 "child"。
当我检查条件 child instanceof Child
时,它 returns 为假。
然而,child instanceof Parent
returns是真的。
为什么会这样?
编辑
所以我发现只有在扩展子 class 时出现错误 class 时才会发生这种情况。
让我在下面留下代码示例。
class Child extends Error {
constructor(message) {
super(message);
}
}
const ch = new Child();
console.log(ch instanceof Child);
第二次编辑
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;
}
}
这是一个记录在案的错误:
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.
建议在构造函数中使用 setPrototypeOf
手动调整原型。 PullCreditError
class 的修复如下所示:
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;
}
}
我有扩展父 class 的子 class。
假设我从子 class 中创建了一个新实例 "child"。
当我检查条件 child instanceof Child
时,它 returns 为假。
然而,child instanceof Parent
returns是真的。
为什么会这样?
编辑
所以我发现只有在扩展子 class 时出现错误 class 时才会发生这种情况。 让我在下面留下代码示例。
class Child extends Error {
constructor(message) {
super(message);
}
}
const ch = new Child();
console.log(ch instanceof Child);
第二次编辑
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;
}
}
这是一个记录在案的错误:
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.
建议在构造函数中使用 setPrototypeOf
手动调整原型。 PullCreditError
class 的修复如下所示:
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;
}
}