我有下面的构造函数和SubType原型指向SuperType的实例。当我这样做时,x.isPrototypeOf(SubType.prototype)它会返回false。我很困惑,因为我已明确将其设置x为的原型SubType。有人可以告诉我为什么会这样吗?
function SuperType(){}
function SubType(){}
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
console.log(x.isPrototypeOf(SubType)) // returns false
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns trueRun Code Online (Sandbox Code Playgroud)
SubType是一个功能。您可能要检查的是SubType 实例是否将从继承x:
function SuperType(){}
function SubType(){}
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns trueRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
421 次 |
| 最近记录: |