Javascript 如何检查实例的构造函数

Javascript 如何检查实例的构造函数,javascript,constructor,instanceof,Javascript,Constructor,Instanceof,我使用“new”关键字创建了新实例(“instance1”和“instance2”)。就这样 1.使用“Child.prototype.constructor=Child” function Parent() { } function Child() { Parent.call(this); } Child.prototype = new Parent(); Child.prototype.constructor = Child; var instance1 = new Child(

我使用“new”关键字创建了新实例(“instance1”和“instance2”)。就这样

1.使用“Child.prototype.constructor=Child”

function Parent() {

}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();
Child.prototype.constructor = Child;

var instance1 = new Child();
function Parent() {

}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();

var instance2 = new Child();
2.不带“Child.prototype.constructor=Child”

function Parent() {

}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();
Child.prototype.constructor = Child;

var instance1 = new Child();
function Parent() {

}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();

var instance2 = new Child();
我可以使用'instanceof'关键字检查实例的构造函数

instance1 instanceof Child  // true
instance1 instanceof Parent // true
这个结果是有意义的,因为我清楚地写了'Child.prototype.constructor=Child;'。所以instanceof关键字可以找到这两个构造函数。但是

instance2 instanceof Child  // true
instance2 instanceof Parent // true
。 但这个结果对我来说毫无意义。我预料

instance2 instanceof Child  // false
因为我没有写'Child.prototype.constructor=Child;'


为什么???

instanceof
运算符查找被测试对象的原型链(
\uuuuu proto\uuu
)中是否存在
构造函数.prototype
对象

在您的示例中:

function Parent() {}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();

var instance2 = new Child();    
由于
instance2
是由
Child()
构造函数构造而成,因此
instance2
\uuu proto\uuu
指向
Child()
构造函数的原型对象,即
Child.prototype

当您测试以下各项时:

instance2 instanceof Child

instanceof
操作符将查看
Child.prototype
对象是否存在于
instance2
的prototype链中,这会导致true,因为
instance2
是从
Child()
构造函数构造的。
换言之:

instance2.__proto__ === Child.prototype

以第二个案例为例:

instance2 instanceof Parent
这里还有
instance2
的原型链,即(
\uuuuu proto\uuuu
)有
父对象。原型将计算为true。i、 e

instance2.__proto__.__proto__ === Parent.prototype

最后一个注释:

function Parent() {}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();

var instance2 = new Child();    

instanceof
操作符的工作原理非常类似于上面的条件检查,用于测试对象是否是构造函数的实例。测试时,
instanceof
运算符从不使用构造函数的
prototype
对象上存在的
constructor
属性


希望这能有所帮助。

instanceof
操作符查找被测试对象的原型链(
\uuuu proto\uu
)中是否存在
构造函数.prototype
对象

在您的示例中:

function Parent() {}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();

var instance2 = new Child();    
由于
instance2
是由
Child()
构造函数构造而成,因此
instance2
\uuu proto\uuu
指向
Child()
构造函数的原型对象,即
Child.prototype

当您测试以下各项时:

instance2 instanceof Child

instanceof
操作符将查看
Child.prototype
对象是否存在于
instance2
的prototype链中,这会导致true,因为
instance2
是从
Child()
构造函数构造的。
换言之:

instance2.__proto__ === Child.prototype

以第二个案例为例:

instance2 instanceof Parent
这里还有
instance2
的原型链,即(
\uuuuu proto\uuuu
)有
父对象。原型将计算为true。i、 e

instance2.__proto__.__proto__ === Parent.prototype

最后一个注释:

function Parent() {}

function Child() {
  Parent.call(this);
}

Child.prototype = new Parent();

var instance2 = new Child();    

instanceof
操作符的工作原理非常类似于上面的条件检查,用于测试对象是否是构造函数的实例。测试时,
instanceof
运算符从不使用构造函数的
prototype
对象上存在的
constructor
属性


希望这有帮助。

谢谢,但我还是不清楚<代码>实例2.\uuuu proto\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。所以我可以理解instance2是链接到“父”的,但我认为“instance2”和“子”构造函数之间没有联系。但是
instance2 instanceof Child
返回“true”。如何知道instance2链接到Child.谢谢,但我还是不清楚<代码>实例2.\uuuu proto\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。所以我可以理解instance2是链接到“父”的,但我认为“instance2”和“子”构造函数之间没有联系。但是
instance2 instanceof Child
返回“true”。如何知道指向子对象的instance2链接。请参阅-请参阅-