简体   繁体   English

有人可以在javascript中解释这种行为吗?

[英]Can someone explain this behavior in javascript?

Tested in Firefox: 在Firefox中测试:

a = [] + 0;
b = 0;

alert(a); // 0
alert(b); // 0

alert(!a); // false
alert(!b); // true

The + concatenation operator causes a toString evaluation of 0 . + 连接运算符导致toString评估为0

As such the value of a is "0" , while the value of b is 0 . 因此, a值为"0" ,而b值为0

From ECMAScript 11.6.1 The Addition operator(+) 来自ECMAScript 11.6.1加法运算符(+)

The addition operator either performs string concatenation or numeric addition. 加法运算符执行字符串连接或数字加法。 The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows: 生产AdditiveExpression:AdditiveExpression + MultiplicativeExpression的计算方法如下:

  1. Let lref be the result of evaluating AdditiveExpression. 让lref成为评估AdditiveExpression的结果。

  2. Let lval be GetValue(lref). 设lval为GetValue(lref)。

  3. Let rref be the result of evaluating MultiplicativeExpression. 设rref是评估MultiplicativeExpression的结果。

  4. Let rval be GetValue(rref). 设rval为GetValue(rref)。

  5. Let lprim be ToPrimitive(lval). 设lprim为ToPrimitive(lval)。

  6. Let rprim be ToPrimitive(rval). 设rprim为ToPrimitive(rval)。

  7. If Type(lprim) is String or Type(rprim) is String, then 如果Type(lprim)是String或Type(rprim)是String,那么

    a. 一个。 Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim) 返回串联ToString(lprim)后跟ToString(rprim)的结果的字符串

  8. Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). 将添加操作的结果返回到ToNumber(lprim)和ToNumber(rprim)。 See the Note below 11.6.3. 见11.6.3下面的注释。

NOTE 1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All native ECMAScript objects except Date objects handle the absence of a hint as if the hint Number were given; 注1:在步骤5和6中对ToPrimitive的调用中没有提供提示。除Date对象之外的所有本机ECMAScript对象都处理没有提示,就像提供了提示号一样; Date objects handle the absence of a hint as if the hint String were given. 日期对象处理缺少提示,就像提供了提示字符串一样。 Host objects may handle the absence of a hint in some other manner. 主机对象可以以某种其他方式处理缺少提示。

NOTE 2 Step 7 differs from step 3 of the comparison algorithm for the relational operators (11.8.5), by using the logical-or operation instead of the logical-and operation. 注2:步骤7与关系运算符(11.8.5)的比较算法的步骤3的不同之处在于,使用逻辑或运算代替逻辑运算。

[] + 0 is a string. [] + 0是一个字符串。
When you write a + b in Javascript, the engine will convert both a and b to primitives. 当您在Javascript中编写a + b时,引擎会将ab都转换为基元。
If at least one of the resulting primitives is a string, it will perform string concatenation; 如果至少有一个结果基元是一个字符串,它将执行字符串连接; otherwise, it will perform numeric addition. 否则,它将执行数字加法。

To convert an object (such as an array) to a primitive, the engine will call valueOf() , and, if its result is not a primitive, will call toString() . 要将对象(例如数组)转换为基元,引擎将调用valueOf() ,如果其结果不是基元,则将调用toString()
For arrays, valueOf() returns the original array (which is not a primitive) and toString() returns comma-separated string of the array's contents. 对于数组, valueOf()返回原始数组(不是基元), toString()返回数组内容的逗号分隔字符串。

Therefore, [] + 0 becomes "" + 0 , which is "0" . 因此, [] + 0变为"" + 0 ,即"0"

Non-empty strings are never falsy. 非空字符串永远不会伪造。

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

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