JavaScript 类型转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4626361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:20:41  来源:igfitidea点击:

JavaScript type casting

javascripttypescasting

提问by Evgenyt

Consider empty JavaScript array:

考虑空的 JavaScript 数组:

var a = [];
alert(a == false); // shows true
alert(!a); // shows false!

How to explain this? What are the rules?

这怎么解释?都有些什么样的规矩?

回答by Ryan Li

From http://forums.whirlpool.net.au/archive/966449:

来自http://forums.whirlpool.net.au/archive/966449

a == false:

a == false

In this case, the type of the left-hand side is object, the type of the right-hand side is boolean. Javascript first converts the boolean to a number, yielding 0. Then it converts the object to a "primitive", yielding the empty string. Next it compares the empty string to 0. The empty string is converted to a number, yielding 0, which is numerically equal to the 0on the right-hand side, so the result of the entire expression is true.

在这种情况下,左侧的类型是对象,右侧的类型是布尔值。Javascript 首先将布尔值转换为数字,产生0. 然后它将对象转换为“原始”,产生空字符串。接下来,它将空字符串与0. 空字符串被转换为一个数字,产生0,它在数字上等于0右边的 ,所以整个表达式的结果是true

See §11.9.3 of the ECMAScript specfor all the gory details.

有关所有血腥细节,请参阅ECMAScript 规范的§11.9.3 。

(!a):

(!a)

In this case Javascript converts the object to the boolean true, then inverts it, resulting in false.

在这种情况下,Javascript 将对象转换为布尔值 true,然后将其反转,结果为 false。

回答by SLaks

The !operator checks whether its operand is "falsy".

!运营商检查是否它的操作数是“falsy”。

The following are true:

以下是正确的:

  • !false
  • !0
  • !null
  • !NaN
  • !undefined
  • !""
  • !false
  • !0
  • !null
  • !NaN
  • !undefined
  • !""


The ==operator checks for loose equality, which has nothing to do with falsiness.

==运营商检查松动的平等,它有无关falsiness。

Specifically, a == bwill convert to operands to numbers, then compare the numbers.
Strings containing numbers convert to the numbers that they contain; booleans convert to 0and 1.
Objects are converted by calling valueOf, if defined.

具体来说,a == b将操作数转换为数字,然后比较数字。
包含数字的字符串转换为它们包含的数字;布尔值转换为01
对象通过调用 进行转换(valueOf如果已定义)。

Thus, all of the following are true:

因此,以下所有情况都是正确的:

  • "1" == 1
  • "0" == false
  • "1" == true
  • "2" != true
  • "2" != false
  • ({ valueOf:function() { return 2; } }) == 2
  • ({ valueOf:function() { return 1; } }) == true
  • "1" == 1
  • "0" == false
  • "1" == true
  • "2" != true
  • "2" != false
  • ({ valueOf:function() { return 2; } }) == 2
  • ({ valueOf:function() { return 1; } }) == true

回答by CMS

The ==operator when one of the operands if Boolean, type-converts the other to Number.

==当操作数之一为布尔值时的运算符,将另一个操作数类型转换为数字。

[] == 0;

Is equivalent to:

相当于:

0 == 0;

You can see the complete details of The Abstract Equality Comparison Algorithmon the specification.

您可以在规范上查看抽象平等比较算法的完整详细信息。

As you can see, an empty array object, when converted to Number, produces 0:

如您所见,一个空数组对象在转换为 Number 时会产生0

+[]; // 0
Number(0);

This is really because its toString method produces an empty string, for example:

这真的是因为它的 toString 方法产生了一个空字符串,例如:

[].toString(); // ""

+""; // 0
Number(""); // 0

回答by ?ime Vidas

When comparing an object to a primitive value via the ==operator, the object coerces into an primitive value itself (number or string). In this case []coerces into 0, then falsecoerces into 0:

当通过==运算符将对象与原始值进行比较时,对象会强制转换为原始值本身(数字或字符串)。在这种情况下,[]强制转换为0,然后false强制转换为0

[] == false
0 == false
0 == 0

which is true.

这是真的。

The !operator coerces into boolean and then inverts the value. []into boolean is true(like with any object). Then invert to become false

!运营商将强制转换布尔然后反转值。[]into boolean is true(就像任何对象一样)。然后反转成为false

![]
!true
false

回答by BishopZ

Not sure if this answers the question, but there is a new library for getting around all of Javascript's Typecasting weirdnesses:

不确定这是否回答了这个问题,但是有一个新的库可以解决所有 Javascript 类型转换的奇怪之处:

Typecast.js

类型转换.js

In a sentence, Typecast solves all the simple problems, so you can focus on the big ones. Typecast fixes what's wrong with Javascript by creating a complete platform for strongly-typed variables in Javascript.

一句话,Typecast 解决了所有简单的问题,让你可以专注于大问题。Typecast 通过为 Javascript 中的强类型变量创建一个完整的平台来修复 Javascript 的问题。