啥? ++[[]][+[]]+[+[]] = 10?
咱先把这个表达式分开:
++[[]][+[]]
+
[+[]]
在JavaScript中, 以下等式是true +[]===0. + 会把一些字符转换成数字,
在这里它会变成 +"" or 0 (具体规则例如以下).
咱能够把它简化成这样 (++ 的优先级比 +更高):
++[[]][0]
+
[0]
因此 [[]][0] 意味着:
获取 [[]] 的第一个元素,实际上:
-
[[]][0]返回内部的数组 ([]). 依据语言规范,说[[]][0]===[]是不正确的,咱把内部这个数组称为A以免误会. -
++[[]][0]==A+1, 由于++意味着 '加1'. -
++[[]][0]===+(A+1); 换句话说,这玩意儿得到的永远是个数值 (+1不一定返回一个数值, 但++一定是).
咱能够让表达式更简化. 用 [] 替换 A:
+([] + 1)
+
[0]
在JavaScript中, 这个等式也是true: []+1==="1",
由于[]=="" (连接一个空数组),
so:
-
+([]+1)===+(""+1), and -
+(""+1)===+("1"), and -
+("1")===1
还能够更简化:
1
+
[0]
在 JavaScript中这个等式 [0]=="0",
也是true,由于这相当于一个 有一个元素的数组内部元素的连接。连接各元素使用 ,切割,当仅仅有一个元素时,能够推出结果就是这个元素本身。
故此, 我们终于得出 (number + string = string):
1
+
"0"
==="10" // 耶!
+[]的具体规则例如以下:
对于 +[],
首先会被转化成一个 string 由于这是 + 规定的:
11.4.6 一元运算符 +
+ 运算符会把操作数转化成 Number 类型.
以下这个一元表达式 : + 一元表达式 会进行例如以下操作:
令 expr 为一元表达式的结果.
返回 ToNumber(GetValue(expr)).
ToNumber() :
Object
应用例如以下步骤:
令 primValue 为 ToPrimitive(input argument, hint String).
返回 ToString(primValue).
ToPrimitive() :
Object
返回这个Object的默认值。默认值咋算呢,通过调用 [[DefaultValue]] 这个 object的内部方法, passing the optional hint PreferredType. [[DefaultValue]] 方法定义见 ECMAScript objects 中 8.12.8.
[[DefaultValue]] :
8.12.8 [[DefaultValue]] (hint)
当object O内部方法 [[DefaultValue]] 被调用时, 会运行例如以下操作: (不翻译了,各位自己感兴趣的话自己看吧)
Let toString be the result of calling the [[Get]] internal method of object O with argument "toString".
If IsCallable(toString) is true then,
a. Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and an empty argument list.
b. If str is a primitive value, return str.
The .toString of
an array says:
15.4.4.2 Array.prototype.toString ( )
When the toString method is called, the following steps are taken:
Let array be the result of calling ToObject on the this value.
Let func be the result of calling the [[Get]] internal method of array with argument "join".
If IsCallable(func) is false, then let func be the standard built-in method Object.prototype.toString (15.2.4.2).
Return the result of calling the [[Call]] internal method of func providing array as the this value and an empty arguments list.
So +[] comes
down to +"",
because [].join()==="".
Again, the + is
defined as:
11.4.6 Unary + Operator
The unary + operator converts its operand to Number type.
The production UnaryExpression : + UnaryExpression is evaluated as follows:
Let expr be the result of evaluating UnaryExpression.
Return ToNumber(GetValue(expr)).
ToNumber is
defined for "" as:
The MV of StringNumericLiteral ::: [empty] is 0.
So +""===0,
and thus +[]===0.

浙公网安备 33010602011771号