码迷,mamicode.com
首页 > 其他好文 > 详细

Implicitly: Strings <--> Numbers

时间:2019-04-24 00:01:54      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:otherwise   cat   nat   notice   ber   ati   rand   fir   other   

According to ES5 spec section 11.6.1, the + algorithm (when an object value is an operand) will concatenate if either operand is either already a string, or if the following steps produce a string representation. So, when + receives an object (including array) for either operand, it first calls the ToPrimitive abstract operation (section 9.1) on the value, which then calls the [[DefaultValue]] algorithm (section 8.12.8) with a context hint of number.

If you‘re paying close attention, you‘ll notice that this operation is now identical to how the ToNumber abstract operation handles objects (see the "ToNumber"" section earlier). The valueOf() operation on the array will fail to produce a simple primitive, so it then falls to a toString() representation. The two arrays thus become "1,2" and "3,4", respectively. Now, + concatenates the two strings as you‘d normally expect: "1,23,4".

var a = [1,2];
var b = [3,4];

a + b; // "1,23,4"

simplified explanation: if either operand to + is a string (or becomes one with the above steps!), the operation will be string concatenation. Otherwise, it‘s always numeric addition.

Implicitly: Strings <--> Numbers

标签:otherwise   cat   nat   notice   ber   ati   rand   fir   other   

原文地址:https://www.cnblogs.com/zlv2snote/p/10759780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!