Javascript中isPrototypeOf和instanceof有什么区别?
在我自己的一些旧代码中,我使用以下内容:
Object.prototype.instanceOf = function( iface )
{
return iface.prototype.isPrototypeOf( this );
};
然后我这样做(例如)
[].instanceOf( Array )
这有效,但似乎以下内容会做同样的事情:
[] instanceof Array
现在,这当然只是一个非常简单的例子。因此,我的问题是:
a instanceof b
ALWAYS 是否与 b.prototype.isPrototypeOf(a)
相同?
In some of my own older code, I use the following:
Object.prototype.instanceOf = function( iface )
{
return iface.prototype.isPrototypeOf( this );
};
Then I do (for example)
[].instanceOf( Array )
This works, but it seems the following would do the same:
[] instanceof Array
Now, surely this is only a very simple example. My question therefore is:
Is a instanceof b
ALWAYS the same as b.prototype.isPrototypeOf(a)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,他们做同样的事情,都遍历原型链寻找其中的特定对象。
两者之间的区别在于它们是什么以及如何使用它们,例如
isPrototypeOf
是Object.prototype
对象上可用的函数,它可以让您测试是否 >一个特定对象位于另一个对象的原型链中,由于该方法是在Object.prototype
上定义的,因此它可用于所有对象。instanceof
是一个运算符 它需要两个操作数,一个对象和一个 构造函数,它将测试传递的函数prototype
属性是否存在于对象链上(通过[[HasInstance]](V)
内部操作,仅在 Function 对象中可用)。例如:
Yes, they do the same thing, both traverse up the prototype chain looking for an specific object in it.
The difference between both is what they are, and how you use them, e.g. the
isPrototypeOf
is a function available on theObject.prototype
object, it lets you test if an specific object is in the prototype chain of another, since this method is defined onObject.prototype
, it is be available for all objects.instanceof
is an operator and it expects two operands, an object and a Constructor function, it will test if the passed functionprototype
property exists on the chain of the object (via the[[HasInstance]](V)
internal operation, available only in Function objects).For example:
不,
a instanceof b
的行为并不总是与b.prototype.isPrototypeOf(a)
相同。CMS的回答指出它们的不同之处在于它们是什么(一个是运算符,另一个是可用的内置方法在
Object.prototype
对象上)。这是正确的,但是也有一些特殊情况,在b.prototype.isPrototypeOf(a)
时a instanceof b
会导致TypeError
> 会工作得很好,反之亦然。区别#1
instanceof
的右侧应该是一个构造函数。如果
b
不是函数:a instanceof b
将导致TypeError
。b.prototype.isPrototypeOf(a)
会工作得很好。差异#2
当使用
b.prototype.isPrototypeOf(a)
时,b.prototype
应该继承自Object.prototype
:如果
b.prototype
无法访问Object.prototype.isPrototypeOf()
方法:b.prototype.isPrototypeOf(a)
将导致类型错误
。a instanceof b
可以正常工作。差异 #3
如果
instanceof
的右侧是绑定函数,则将其视为与其目标函数等效。如果 b 是一个绑定函数:
a instanceof b
就可以正常工作。b.prototype.isPrototypeOf(a)
将导致TypeError
(绑定函数没有prototype
属性)。结论
Object.create()
建立的原型继承,而不使用构造函数,那么您可能应该使用Object.prototype.isPrototypeOf()
方法(实际上,instanceof
的用例受到更多限制,因为instanceof
期望其右侧参数是构造函数)。instanceof
运算符会稍微安全一些(您将能够覆盖绑定函数以及Object.prototype
不支持的情况)位于Constructor.prototype
的原型链中)。No,
a instanceof b
will not always behave the same asb.prototype.isPrototypeOf(a)
.CMS' answer pointed out that they differ in what they are (one is an operator and the other is a built-in method available on the
Object.prototype
object). This is correct, however there are also some special cases for whicha instanceof b
will result in aTypeError
whileb.prototype.isPrototypeOf(a)
will work just fine and vice versa.Difference #1
The right-hand side of
instanceof
is expected to be a constructor function.If
b
is not a function:a instanceof b
will result in aTypeError
.b.prototype.isPrototypeOf(a)
will work just fine.Difference #2
When using
b.prototype.isPrototypeOf(a)
,b.prototype
should be inheriting fromObject.prototype
:If
b.prototype
has not access to theObject.prototype.isPrototypeOf()
method:b.prototype.isPrototypeOf(a)
will result in aTypeError
.a instanceof b
will work just fine.Difference #3
If the right-hand side of
instanceof
is a bound function, it is treated equivalently to its target function.If b is a bound function:
a instanceof b
will work just fine.b.prototype.isPrototypeOf(a)
will result in aTypeError
(bound functions don't have aprototype
property).Conclusion
Object.create()
, without the use of constructors, you should probably be using theObject.prototype.isPrototypeOf()
method (indeed the use cases ofinstanceof
are more restricted in thatinstanceof
expects its right-hand side parameter to be a constructor function).instanceof
operator (you will be able to cover bound functions as well as the cases whereObject.prototype
does not lie in the prototype chain ofConstructor.prototype
).运算符优先级和真实性不同,因为一个是表达式,另一个是方法调用。需要强调的一件事是,两者都遍历原型链,因此您不能假设匹配的原型和相关对象之间存在一对一的映射:
参考
Operator precedence and truthiness differ since one is an expression and the other is a method call. One thing to emphasize is that both traverse the prototype chain, so you cannot assume that there is a one-to-one mapping between a matching prototype and the object in question:
References