Source: http://stackoverflow.com/questions/7202157/why-is-10
If we split it up, the mess is equal to:
++[[]][+[]] + [+[]]
In JavaScript, it is true that +[] === 0. + converts something into a number, and in this case it will come down to +"" or 0 (see specification details below).
Therefore, we can simplify it (++ has precendence over +):
++[[]][0] + [0]
Because [[]][0] means: get the first element from [[]], it is true that:
[[]][0] returns the inner array ([]). Due to references it"s wrong to say [[]][0] === [], but let"s call the inner array A to avoid wrong notation.
++[[]][0] == A + 1, since ++ means "increment by one".
++[[]][0] === +(A + 1); in other words, it will always be a number (+1 does not necessarily return a number, whereas ++ always does - thanks to Tim Down for pointing this out).
Again, we can simplify the mess into something more legible. Let"s substitute [] back for A:
+([] + 1) + [0]
In JavaScript, this is true as well: [] + 1 === "1", because [] == "" (joining an empty array), so:
+([] + 1) === +("" + 1), and
+("" + 1) === +("1"), and
+("1") === 1
Let"s simplify it even more:
1 + [0]
Also, this is true in JavaScript: [0] == "0", because it"s joining an array with 1 element. Joining will concatenate the elements separated by ,. With one element, you can deduce that this logic will result in the first element itself.
So, in the end we obtain (number + string = string):
1 + "0" === "10" // Yay!
Specification details for +[]:
This is quite a maze, but to do +[], first it is being converted to a string because that"s what + says:
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() says:
Object
Apply the following steps:
Let primValue be ToPrimitive(input argument, hint String).
Return ToString(primValue).
ToPrimitive() says:
Object
Return a default value for the Object. The default value of an object is retrieved by calling the [[DefaultValue]] internal method of the object, passing the optional hint PreferredType. The behaviour of the [[DefaultValue]] internal method is defined by this specification for all native ECMAScript objects in 8.12.8.
[[DefaultValue]] says:
8.12.8 [[DefaultValue]] (hint)
When the [[DefaultValue]] internal method of O is called with hint String, the following steps are taken:
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.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/85289.html
摘要:許多的頂尖研究人員都會(huì)積極的在現(xiàn)場(chǎng)回答問(wèn)題。雖然有許多主題的常見(jiàn)問(wèn)題頁(yè)面比如,這是一個(gè)機(jī)器學(xué)習(xí)的,但是這些都是非常不全面的,或者不夠精致。在這篇文章中,我試圖做一個(gè)更加全面的有關(guān)機(jī)器學(xué)習(xí)和問(wèn)題的。 作者:chen_h微信號(hào) & QQ:862251340微信公眾號(hào):coderpai簡(jiǎn)書(shū)地址:http://www.jianshu.com/p/ac18... showImg(https:/...
摘要:在這堂課中,學(xué)生將可以學(xué)習(xí)到深度學(xué)習(xí)的基礎(chǔ),學(xué)會(huì)構(gòu)建神經(jīng)網(wǎng)絡(luò),包括和等。課程中也會(huì)有很多實(shí)操項(xiàng)目,幫助學(xué)生更好地應(yīng)用自己學(xué)到的深度學(xué)習(xí)技術(shù),解決真實(shí)世界問(wèn)題。 深度學(xué)習(xí)入門首推課程就是吳恩達(dá)的深度學(xué)習(xí)專項(xiàng)課程系列的 5 門課。該專項(xiàng)課程最大的特色就是內(nèi)容全面、通俗易懂并配備了豐富的實(shí)戰(zhàn)項(xiàng)目。今天,給大家推薦一份關(guān)于該專項(xiàng)課程的核心筆記!這份筆記只能用兩個(gè)字形容:全面! showImg(...
from http://oyanglul.us Im a Emacs user and as you may know there is a awesome mode called gist.el, and since then, I found that its completely...
閱讀 3596·2021-09-13 10:28
閱讀 1946·2021-08-10 09:43
閱讀 1018·2019-08-30 15:44
閱讀 3189·2019-08-30 13:14
閱讀 1844·2019-08-29 16:56
閱讀 2947·2019-08-29 16:35
閱讀 2853·2019-08-29 12:58
閱讀 872·2019-08-26 13:46