3

(3).constructor是Number,但是"constructor" in (3)是TypeError,为什么?

4

2 回答 2

3

操作员的规范in解释了这一点。具体参见语义评估的第 5 步。

11.8.7 in 运算符

产生式 RelationalExpression : RelationalExpression 中的 ShiftExpression 评估如下:

  1. 令 lref 为评估 RelationalExpression 的结果。
  2. 令 lval 为 GetValue(lref)。
  3. 令 rref 为计算 ShiftExpression 的结果。
  4. 设 rval 为 GetValue(rref)。
  5. 如果 Type(rval) 不是 Object,则抛出TypeError异常。
  6. 返回使用参数 ToString(lval) 调用 rval 的 [[HasProperty]] 内部方法的结果。

工作的原因(3).constructor有点难以理解,但本质上,当您在具有原始基础的引用上使用点运算符时,它会被提升到对象中。

于 2010-11-04T13:39:34.397 回答
3

With using the property accessor, the number 3 is turned into an object of Number. And an object of that type has a constructor and thus an constructor property.

But as ChaosPandion already pointed out, the in operator requires the right expression to yield an object but 3 is not an object but a plain number literal.

于 2010-11-04T13:47:34.690 回答