JavaScript表达式[1 [{}]]究竟是如何解析的? [英] How exactly does the JavaScript expression [1 [{}]] parse?
问题描述
你能解释一下JavaScript表达式:
Can you explain how the JavaScript expression:
[1 [{}]]
解析/评估?在Firefox,Chrome,Konqueror和rhino中,它似乎创建了一个包含单个元素的数组, undefined
。但是,我不明白为什么。
parses/evaluates? In Firefox, Chrome, Konqueror, and rhino, it seems to create an array with a single element, undefined
. However, I don't understand why.
在Firefox中:
[1 [{}]].toSource()
产生
[(void 0)]
用其他JavaScript值替换1似乎产生相同的结果。
Replacing 1 with other JavaScript values seems to yield the same result.
更新:我想我现在明白了。 codeka,Adrian和CMS澄清了一些事情。至于标准,我试图通过ECMAScript 5。
Update: I think I understand now. codeka, Adrian, and CMS clarified things. As far as the standard, I tried to walk through ECMAScript 5.
-
1 [{}]
是一个属性访问器,因此它包含在§11.2.1中。 -
baseReference
是评估<$的结果c $ c> 1 ,所以仍然1
。 -
baseValue = GetValue (baseReference)== 1
。 - 在
GetValue
(§8.7.1),类型(1)
不是参考
(已解析的名称绑定),因此返回1. -
propertyNameReference
是评估{}
的结果,因此是一个空对象。 -
propertyNameValue = GetValue(propertyNameReference)== {}
- 在
CheckObjectCoercible(baseValue)
(§9.10),我们返回(Number是对象可强制的)。 -
propertyNameString = ToString(propertyNameValue)
- 在
ToString
(§9.8),返回ToString(ToPrimitive({},提示字符串))
-
T oPrimitive
(§9.1),返回对象的[[DefaultValue]]
的结果,传递PreferredType
(string)。 - 在
[[DefaultValue]]
(§8.12.8)中,让toString为<$ c的结果$ c> [[Get]] 参数toString
。 - 这在§15.2中定义。 4.2返回
[object+ [[Class]] +]
,其中[[Class]]
是默认对象原型的对象。 - 由于有一个可调用的
toString
,我们用参数调用它
{}
。 - 返回类型
参考
的值,其基数为value是BaseValue
(1),其引用名称是propertyNameString
([object Object]
)。
1 [{}]
is a Property Accessor, so it's covered in §11.2.1.baseReference
is the result of evaluating1
, so still1
.baseValue = GetValue(baseReference) == 1
.- At
GetValue
(§8.7.1),Type(1)
is notReference
(a resolved name binding), so return 1. propertyNameReference
is result of evaluating{}
, so an empty object.propertyNameValue = GetValue(propertyNameReference) == {}
- At
CheckObjectCoercible(baseValue)
(§9.10), we return (Number is object-coercible). propertyNameString = ToString(propertyNameValue)
- At
ToString
(§9.8), returnToString(ToPrimitive({}, hint String))
- At
ToPrimitive
(§9.1), return result of object's[[DefaultValue]]
, passingPreferredType
(string). - At
[[DefaultValue]]
(§8.12.8), let toString be result of[[Get]]
with argumenttoString
. - This is defined at §15.2.4.2 to return
"[object " + [[Class]] + "]"
, where[[Class]]
is "Object" for the default object prototype. - Since there is a callable
toString
, we call it with argumentthis
being{}
. - Return a value of type
Reference
, whose base value isBaseValue
(1) and whose referenced name ispropertyNameString
("[object Object]"
).
然后我们转到Array初始化器(第11.1.4节)并构造一个带有结果的元素数组。
We then go to Array initializer (§11.1.4), and construct a single element array with the result.
推荐答案
如果我们稍微分解一下,你会看到:
If we break it up a bit, you'll see:
var foo = 1;
var bar = {};
var baz = foo[bar];
[baz];
我相信它是有效的JavaScript,但我不是专家......
I believe it's valid JavaScript, but I'm not an expert...
这篇关于JavaScript表达式[1 [{}]]究竟是如何解析的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!