简体   繁体   English

空JavaScript阵列的布尔值冲突

[英]Conflicting boolean values of an empty JavaScript array

Can anyone explain why the following two statements both evaluate as true ? 任何人都可以解释为什么以下两个陈述都评价为

[] == false

and

!![]

This question is purely out of curiosity of why this happens and not about how to best test if an array is empty. 这个问题完全出于好奇,为什么会发生这种情况而不是如何最好地测试数组是否为空。

The first one: 第一个:

[] == false

The == operator does type conversion to its operands, in this case the both sides are converted to Number, the steps taken on the Abstract Equality Comparison Algorithm would be: ==运算符确实键入到其操作数的转换,在这种情况下,双方都转换为Number, 抽象等式比较算法采取的步骤是:

  • object == boolean object == boolean
  • object == number object == number
  • string == number string == number
  • number == number number == number

In code: 在代码中:

[] == false; // convert false to Number
[] == 0;     // convert [] to Primitive (toString/valueOf)
"" == 0;     // convert "" to Number
0  == 0;     // end

The second comparison, [] is converted to primitive, their valueOf and toString methods are executed, but since valueOf on Array objects, returns the object itself (is inherited from Object.prototype ), then the toString method is used. 第二个比较, []转换为原始,它们的valueOftoString方法被执行,但是由于Array对象上的valueOf返回对象本身(从Object.prototype继承),然后使用toString方法。

At the end as you see, both operands are converted to Number, and both yield zero, for example: 如您所见,两个操作数都转换为Number,并且两者都为零,例如:

Number([]) == 0;
Number(false) == 0;

And empty array produces zero when converted to Number because its string representation is an empty string: 转换为Number时,空数组产生零,因为其字符串表示形式为空字符串:

[].toString(); // ""

And an empty string converted to Number, yields zero: 并且一个空字符串转换为Number,产生零:

+""; // 0

Now, the double negation ( !![] ) produces true because all object instances are truthy: 现在,双重否定( !![] )产生true,因为所有对象实例都是真实的:

![];  // false, [] is truthy
!![]; // true, negation

The only values that are falsey are: 唯一值得假的是:

  • null
  • undefined
  • 0
  • NaN
  • "" (an empty string) "" (空字符串)
  • false

Anything else will produce true when converted to Boolean. 任何东西都不会产生true当转换为布尔。

See also: 也可以看看:

[] == false [] == false

In this case, the type of the left-hand side is object, the type of the right-hand side is boolean. 在这种情况下,左侧的类型是对象,右侧的类型是布尔值。 When object is compared to (== The Abstract Equality Comparison) boolean, Javascript first converts the boolean to a number, yielding 0. Then it converts the object to a "primitive", yielding the empty string "". 当将对象与(==抽象等式比较)布尔值进行比较时,Javascript首先将布尔值转换为数字,得到0.然后它将对象转换为“基元”,产生空字符串“”。 Next it compares the empty string to 0. The empty string is converted to a number, yielding 0, which is numerically equal to the 0 on the right-hand side, so the result of the entire expression is true. 接下来,它将空字符串与0进行比较。空字符串转换为数字,产生0,在数字上等于右侧的0,因此整个表达式的结果为true。

Ref: http://es5.github.com/#x11.9.3 11.9.3 The Abstract Equality Comparison Algorithm 参考: http ://es5.github.com/#x11.9.3 11.9.3抽象等式比较算法

!![] !! []

In this case Javascript converts the object to the boolean true, then inverts it, resulting in false. 在这种情况下,Javascript将对象转换为布尔值true,然后将其反转,结果为false。

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

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