为什么if(" string")评估" string"如果为真,但if(" string" == true)不是? [英] why does if("string") evaluate "string" as true but if ("string"==true) does not?

查看:121
本文介绍了为什么if(" string")评估" string"如果为真,但if(" string" == true)不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

if ("string") {
    console.log('true!');
}
//logs "true" to the console
if ("string"==true) {
    console.log('true!');
}
//doesn't log anything

为什么会这样?我认为string被强制转换为数字,布尔值也是如此。所以 true 变为 1 ,而string变为的NaN 。第二个if语句有意义,但我不明白为什么第一个语句会导致内部循环被评估。这里发生了什么?

Why does this happen? I thought "string" was being cast to a number, as is the boolean. So true becomes 1, and "string" becomes NaN. The second if statement makes sense, but I don't see why the first statement causes the inner loop to be evaluated. What's going on here?

推荐答案

它被强制转换为布尔值。任何非空字符串的计算结果为true。

It is being cast to Boolean. Any non-empty string evaluates to true.

来自ECMAScript语言规范


12.5 if 声明



语义



生产 IfStatement if( Expression Statement else Statement 评估如下:

12.5 The if statement

Semantics

The production IfStatement: if ( Expression ) Statement else Statement is evaluated as follows:


  1. exprRef 是评估表达式的结果。

  2. 如果ToBoolean(GetValue( exprRef ))是 true ,然后

    • 返回评估第一个 Statement 的结果。

  1. Let exprRef be the result of evaluating Expression.
  2. If ToBoolean(GetValue(exprRef)) is true, then
    • Return the result of evaluating the first Statement.

  • 返回评估第二个语句的结果

  • Return the result of evaluating the second Statement.



9.2 ToBoolean



抽象操作ToBoolean根据表11将其参数转换为Boolean类型的值:

9.2 ToBoolean

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

未定义: false

Null: false

布尔值:结果等于输入参数(无转换) )。$
数字:如果参数为 +0 -0 ,则结果为 false 为NaN ;否则结果为 true

字符串:如果参数为空字符串(其长度为零),则结果为 false ;
否则结果为 true

对象: true

Undefined: false
Null: false
Boolean: The result equals the input argument (no conversion).
Number: The result is false if the argument is +0, -0, or NaN; otherwise the result is true.
String: The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
Object: true






== 运算符而言,它很复杂,但它的要点是如果将数字与非数字进行比较,后者将转换为数字。如果将布尔值与非布尔值进行比较,则首先将布尔值转换为数字,然后应用前一句子。


As far as the == operator is concerned, it's complicated, but the gist of it is that if you compare a number to a non-number the latter is converted into a number. If you compare a boolean against a non-boolean, the boolean is first converted to a number, and then the previous sentence applies.

有关详细信息,请参阅第11.9.3节。

See section 11.9.3 for details.

// Call this x == y.
if ("string" == true) 

// Rule 6: If Type(y) is Boolean,
//         return the result of the comparison x == ToNumber(y).
if ("string" == Number(true))

// Rule 5: If Type(x) is String and Type(y) is Number,
//         return the result of the comparison ToNumber(x) == y.  
if (Number("string") == Number(true))

// The above is equivalent to:
if (NaN == 1)

// And NaN compared to *anything* is false, so the end result is:
if (false)

这篇关于为什么if(" string")评估" string"如果为真,但if(" string" == true)不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆