Javascript instanceof究竟是如何工作的?是慢节奏吗? [英] How exactly does Javascript instanceof work? Is it slow style?
问题描述
大型图书馆的 instanceof
的表现如何公平?
How does the performance of instanceof
fair for "huge libraries"?
它是否沿着原型传播链逐一,类似于此? :
Does it travel up the prototype chain one by one, similar to this? :
//..
var _ = john.constructor;
while (true) {
if (_ === Human) {
return true;
}
_ = _.prototype.constructor
}
return false;
//..
instanceof
相对于在每个对象的属性中存储唯一的接口ID号而言相对不太友好。
Is instanceof
relatively unperfomant then, compared to storing a unique interface id number in the property of every object.
推荐答案
是的类似的东西。以下是规范中的相关部分:
Yeah something like that. Here is the relevant part from the specification:
11.8.6 The instanceof operator
生产 RelationalExpression : RelationalExpression instanceof ShiftExpression 评估如下:
- 让 lref 成为评估 RelationalExpression 的结果。
- 让 lval 为GetValue( lref )。
- 让 rref 成为评估 ShiftExpression 的结果。
- 让 rval 为GetValue( rref )。
- 如果Type( rval )不是Object,则抛出 TypeError 异常。
- 如果 rval 没有[[HasInstance]]内部方法,抛出 TypeError 异常。
- Ret使用参数 lval 调用 rval 的[[HasInstance]]内部方法的结果。
- Let lref be the result of evaluating RelationalExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating ShiftExpression.
- Let rval be GetValue(rref).
- If Type(rval) is not Object, throw a TypeError exception.
- If rval does not have a [[HasInstance]] internal method, throw a TypeError exception.
- Return the result of calling the [[HasInstance]] internal method of rval with argument lval.
其中调用[[HasInstance]]方法定义为
15.3.5.3 [[HasInstance]] (V)
假设 F 是一个Function对象。
Assume F is a Function object.
当[[HasInstance] ]]使用值 V 调用 F 的内部方法,执行以下步骤:
When the [[HasInstance]] internal method of F is called with value V, the following steps are taken:
- 如果 V 不是对象,请返回 false 。
- 让 O 成为使用属性名称原型调用 F 的[[Get]]内部方法的结果。
- If Type( O )不是Object,抛出 TypeError 异常。
- 重复
a。设 V 为 V 的[[Prototype]]内部属性的值。
b。如果 V null ,则返回 false 。
c。如果 O 和 V 引用同一个对象,请返回 true 。
- If V is not an object, return false.
- Let O be the result of calling the [[Get]] internal method of F with property name "prototype".
- If Type(O) is not Object, throw a TypeError exception.
- Repeat
a. Let V be the value of the [[Prototype]] internal property of V.
b. If V is null, return false.
c. If O and V refer to the same object, return true.
关于性能:这可能取决于浏览器中的实际实现。它们之间可能存在巨大差异,因此最好的做法是制定一些基准测试,例如:使用 http://jsperf.com/ 。
Regarding performance: This probably depends on the actual implementations in the browsers. There can be huge differences between them so the best thing would be to make some benchmarks, e.g. with http://jsperf.com/.
instanceof
的问题在于,如果您在来自不同上下文的元素(例如框架或iframe)上调用它,它可能无效。例如,让 a
成为您可以通过 iframe.contentWindow.a
访问的对象,并且您想要测试它是否是一个数组,然后
A problem with instanceof
is that it might not work if you invoke it on elements from different contexts, such as a frame or iframe. For example, let a
be an object you can access via iframe.contentWindow.a
and you want to test whether it is an array, then
iframe.contentWindow.a instanceof Array
将返回 false
。
这篇关于Javascript instanceof究竟是如何工作的?是慢节奏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!