JavaScript isPrototypeOf 与 instanceof 用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18343545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript isPrototypeOf vs instanceof usage
提问by user1689498
Suppose we have the following:
假设我们有以下内容:
function Super() {
// init code
}
function Sub() {
Super.call(this);
// other init code
}
Sub.prototype = new Super();
var sub = new Sub();
Then, in some other part of our ocde, we can use either of the following to check for the relationship:
然后,在我们 ocde 的其他部分,我们可以使用以下任一方法来检查关系:
sub instanceof Super;
or
或者
Super.prototype.isPrototypeOf( sub )
Either way, we need to have both the object (sub), and the parent constructor (Super). So, is there any reason why you'd use one vs the other? Is there some other situation where the distinction is more clear?
无论哪种方式,我们都需要同时拥有对象(子)和父构造函数(超级)。那么,有什么理由让您使用一种与另一种吗?是否还有其他一些区别更清楚的情况?
I've already carefully read 2464426, but didn't find a specific enough answer.
我已经仔细阅读了2464426,但没有找到足够具体的答案。
回答by apsillers
Imagine you don't use constructorsin your code, but instead use Object.create
to generate objects with a particular prototype. Your program might be architected to use no constructors at all:
想象一下,您不在代码中使用构造函数,而是Object.create
用于生成具有特定原型的对象。您的程序可能被设计为根本不使用构造函数:
var superProto = {
// some super properties
}
var subProto = Object.create(superProto);
subProto.someProp = 5;
var sub = Object.create(subProto);
console.log(superProto.isPrototypeOf(sub)); // true
console.log(sub instanceof superProto); // TypeError
Here, you don't have a constructor function to use with instanceof
. You can only use subProto.isPrototypeOf(sub)
.
在这里,您没有可与instanceof
. 您只能使用subProto.isPrototypeOf(sub)
.
回答by Scott Sauyet
It makes little difference when you use constructor functions. instanceof
is a little cleaner, perhaps. But when you don't...:
当您使用构造函数时,它没有什么区别。 instanceof
也许更清洁一点。但是当你不...:
var human = {mortal: true}
var socrates = Object.create(human);
human.isPrototypeOf(socrates); //=> true
socrates instanceof human; //=> ERROR!
So isPrototypeOf
is more general.
所以isPrototypeOf
更一般。
回答by svenskanda
var neuesArray = Object.create(Array);
Array.isPrototypeOf(neuesArray); // true
neuesArray instanceof Array // false
neuesArray instanceof Object // true
Array.isArray(neuesArray); // false
Array.prototype.isPrototypeOf(neuesArray); // false
Object.prototype.isPrototypeOf(neuesArray); // true
Do you understand my friend :) - is simple
你明白我的朋友吗:) - 很简单
回答by S.Serpooshan
According to this MDN Ref:
根据这个MDN 参考:
isPrototypeOf()
differs from theinstanceof
operator. In the expressionobject instanceof AFunction
, the object prototype chain is checked againstAFunction.prototype
, not againstAFunction
itself.
isPrototypeOf()
与instanceof
运营商不同。在表达式中object instanceof AFunction
,对象原型链是针对AFunction.prototype
而不是针对AFunction
自身进行检查的。
回答by Tina Chen
Just complement @apsillers's answer
只需补充@apsillers 的回答
object instanceof constructor
object instanceof constructor
var superProto = {}
// subProto.__proto__.__proto__ === superProto
var subProto = Object.create(superProto);
subProto.someProp = 5;
// sub.__proto__.__proto__ === subProto
var sub = Object.create(subProto);
console.log(superProto.isPrototypeOf(sub)); // true
console.log(sub instanceof superProto); // TypeError: Right-hand side of 'instanceof' is not callable
// helper utility to see if `o1` is
// related to (delegates to) `o2`
function isRelatedTo(o1, o2) {
function F(){}
F.prototype = o2;
// ensure the right-hand side of 'instanceof' is callable
return o1 instanceof F;
}
isRelatedTo( b, a );
TypeError: Right-hand side of 'instanceof' is not callable
类型错误:“instanceof”的右侧不可调用
instanceof
need the right-hand value to be callable, which means it must be a function(MDN call it as the constructor)
instanceof
需要右侧的值是可调用的,这意味着它必须是一个函数(MDN 将其称为构造函数)
and instanceof
tests the presence of constructor.prototype
in object's prototype chain.
并instanceof
测试constructor.prototype
对象原型链中的存在。
but isPrototypeOf()
don't have such limit. While instanceof
checks superProto.prototype
, isPrototypeOf()
checks superProto
directly.
但isPrototypeOf()
没有这样的限制。当instanceof
检查时superProto.prototype
,直接isPrototypeOf()
检查superProto
。