JavaScript表达式[1 [{}]]究竟是如何解析的? [英] How exactly does the JavaScript expression [1 [{}]] parse?

查看:137
本文介绍了JavaScript表达式[1 [{}]]究竟是如何解析的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下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. 1 [{}] 是一个属性访问器,因此它包含在§11.2.1中。

  2. baseReference 是评估<$的结果c $ c> 1 ,所以仍然 1

  3. baseValue = GetValue (baseReference)== 1

  4. GetValue (§8.7.1),类型(1)不是参考(已解析的名称绑定),因此返回1.

  5. propertyNameReference 是评估 {} 的结果,因此是一个空对象。

  6. propertyNameValue = GetValue(propertyNameReference)== {}

  7. CheckObjectCoercible(baseValue)(§9.10),我们返回(Number是对象可强制的)。

  8. propertyNameString = ToString(propertyNameValue)

  9. ToString (§9.8),返回 ToString(ToPrimitive({},提示字符串))

  10. T oPrimitive (§9.1),返回对象的 [[DefaultValue]] 的结果,传递 PreferredType (string)。

  11. [[DefaultValue]] (§8.12.8)中,让toString为<$ c的结果$ c> [[Get]] 参数 toString

  12. 这在§15.2中定义。 4.2返回[object+ [[Class]] +],其中 [[Class]] 是默认对象原型的对象。

  13. 由于有一个可调用的 toString ,我们用参数调用它 {}

  14. 返回类型参考的值,其基数为value是 BaseValue (1),其引用名称是 propertyNameString [object Object] )。

  1. 1 [{}] is a Property Accessor, so it's covered in §11.2.1.
  2. baseReference is the result of evaluating 1, so still 1.
  3. baseValue = GetValue(baseReference) == 1.
  4. At GetValue (§8.7.1), Type(1) is not Reference (a resolved name binding), so return 1.
  5. propertyNameReference is result of evaluating {}, so an empty object.
  6. propertyNameValue = GetValue(propertyNameReference) == {}
  7. At CheckObjectCoercible(baseValue) (§9.10), we return (Number is object-coercible).
  8. propertyNameString = ToString(propertyNameValue)
  9. At ToString (§9.8), return ToString(ToPrimitive({}, hint String))
  10. At ToPrimitive (§9.1), return result of object's [[DefaultValue]], passing PreferredType (string).
  11. At [[DefaultValue]] (§8.12.8), let toString be result of [[Get]] with argument toString.
  12. This is defined at §15.2.4.2 to return "[object " + [[Class]] + "]", where [[Class]] is "Object" for the default object prototype.
  13. Since there is a callable toString, we call it with argument this being {}.
  14. Return a value of type Reference, whose base value is BaseValue (1) and whose referenced name is propertyNameString ("[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆