简体   繁体   English

为什么1+ []在JavaScript中返回“1”?

[英]Why does 1+[ ] return “1” in JavaScript?

Recently one of my friends asked me a question: 最近有一位朋友问我一个问题:

![]+1+[]+false

going with my instincts after evaluation I answered 2 . 在评价后,我以直觉行事,我回答了2 But when I tried the expression in browser console the output I get is "1false" . 但是当我在浏览器控制台中尝试表达式时,我得到的输出是"1false"

for me the weird part is 1+[] which evaluates to "1" ? 对我来说奇怪的部分是1+[] ,评价为"1"

I couldn't understand why is that and how is it working? 我无法理解为什么会这样,它是如何工作的?

Can someone please explain? 有人可以解释一下吗?

Thanks 谢谢

Adding a number and an object, tries to convert the object to a primitive. 添加数字和对象,尝试将对象转换为基元。 To do that, it calls .toString() on the object, the empty array in this case, and for arrays that calls .join() , and [].join() is "" , and thus 1 + [] is the same as "1" . 为此,它在对象上调用.toString() ,在这种情况下调用空数组,对于调用.join()[].join()数组调用"" ,因此1 + []是与"1"相同。

Also keep in mind that additions go from left to right. 另请注意,添加内容从左到右。

 (![]) + 1 + [] + false // objects are truthy, therefore *not object* is false
 (false + 1) + [] + false // boolean gets coerced to number, and false is 0
 (0 + 1) + [] + false // 0 + 1 is 1
 (1 + []) + false // .valueOf() as described above
 (1 + "") + false
 "1" + false
 "1false"

Since JavaScript is a weakly-typed language, values can also be converted between different types automatically when you apply operators to values of different type, and it is called implicit type coercion. 由于JavaScript是一种弱类型语言,因此当您将运算符应用于不同类型的值时,也可以自动在不同类型之间转换值,并将其称为隐式类型强制。

In general the algorithm is as follows: 一般来说算法如下:

  1. If input is already a primitive, do nothing and return it. 如果输入已经是原语,则不执行任何操作并将其返回。
  2. Call input.toString(), if the result is primitive, return it. 调用input.toString(),如果结果是原始的,则返回它。
  3. Call input.valueOf(), if the result is primitive, return it. 调用input.valueOf(),如果结果是原始的,则返回它。
  4. If neither input.toString() nor input.valueOf() yields primitive, throw TypeError. 如果input.toString()和input.valueOf()都不生成原语,则抛出TypeError。

Numeric conversion first calls valueOf (3) with a fallback to toString (2). 数字转换首先调用valueOf(3)并回退到toString(2)。
String conversion does the opposite: toString (2) followed by valueOf (3). 字符串转换相反:toString(2)后跟valueOf(3)。

+ operator triggers numeric conversion. +运算符触发数字转换。

so valueOf will be called for blank array, which is blank array only. 所以valueOf将被调用为空数组,这只是空数组。 [].valueOf() // [] . [].valueOf() // [] Since valueOf is not primitive it will call [].toString() //"" 由于valueOf不是原始的,它将调用[].toString() //""

so 1 + [] will convert to 1 + "" . 所以1 + []将转换为1 + ""

Since one of the operands is string operator triggers string conversion for 1. 由于其中一个操作数是字符串操作符,因此字符串转换为1。

so 1 + "" will convert to "1" + "" 所以1 + ""将转换为"1" + ""

And hence you get the output "1" 因此你得到输出"1"

If at least one operand is string type, the other operand is converted to string and the concatenation is executed. 如果至少一个操作数是字符串类型,则另一个操作数转换为字符串并执行连接。

So in this case 1+[] - 所以在这种情况下1+ [] -

[].toString() gives the empty string and the 1 will be stringified and you get the output "1" [] .toString()给出空字符串,1将被字符串化,你得到输出“1”

further false also will be stringified and "1"+false = "1false" 进一步的错误也将被字符串化并且“1”+ false =“1false”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM