使用==比较整数和字符串时JavaScript中的隐式数据类型转换 [英] Implicit data type conversion in JavaScript when comparing integer with string using ==

查看:185
本文介绍了使用==比较整数和字符串时JavaScript中的隐式数据类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

var num = 20;

if(num == "20")
{
    alert("It works");
}
else
{
    alert("Not working");
}

问题:


  1. 在C编程中,我们有一个规则名称数据类型提升,当有混合数据类型时(例如:添加整数和浮点),整数将首先转换为浮点数在执行添加之前的一点。

  1. In C programming we have a rule name data type promotion, where when there's a mix of data type (example: addition of integer and floating point), the integer will first converted to floating point before the addition is being carry out.

上面的代码会提示我一个警告框,其中包含消息它有效显示,如果测试条件评估为真。

The code above will prompt me an alert box with the message "It works" that shows the if test condition is evaluate to true.

松散地打字的JavaScript,我只是很好奇:有没有像C这样的规则决定哪种转换将在哪种情况下执行?除此之外,上面的JavaScript代码在进行比较之前将 num 变量值从整数值转换为字符串值,反之亦然?

For loosely typed JavaScript, I'm just curious: is there any rule like C that determines which conversion will be carry out in which situation? Besides that, the JavaScript code above converts num variable value from an integer value to string value before making comparison or vice versa?


推荐答案

是的,equals运算符应用的所有类型转换规则都在ECMA-262规范中描述,抽象等式比较算法

Yes, all the rules of type conversion applied by the equals operator are described on the ECMA-262 specification, in The Abstract Equality Comparison Algorithm.

算法可能看起来很复杂但它可归纳为以下情况:

The algorithm might look quite complex but it can be summarized to the following cases:


  1. 两个操作数的类型相同:

  1. The type the two operands is the same:


  • 对于基元(String,Number,Boolean,Null,Undefined)

    • 如果值正好则返回true相同的


    • 如果两个引用点返回true到同一个对象

    如果两个操作数的类型不同

    If the types of the two operands differ


    • 如果一个操作数的类型是Null或Undefined

      • 仅当另一个操作数值为 null undefined

      • If the type of one operand is either Null or Undefined
        • Return true only if the other operand value is either null or undefined

        • (经过一些步骤)将另一个操作数转换为数字并进行比较

        如果其中一个操作数是Object而另一个是原语

        If one of the operands is an Object and the other is a primitive


        • 对对象执行对象到原始的转换并再次比较

        Object-to-Primitive转换是通过一个名为 ToPrimitive ,此方法将尝试使用内部 [[PrimitiveValue]]将对象转换为原始值 方法。

        The Object-to-Primitive conversion is made through an abstract operation called ToPrimitive, this method will try to convert the object to a primitive value, using the internal [[PrimitiveValue]] method.

        这将尝试执行对象的 valueOf a nd toString 方法,它将取第一个返回原始值的值。

        This will try to ejecute the object's valueOf and toString methods, and it will take the value of the first that returns a primitive value.

        在这种情况下这两个方法不返回原语,或者它们不可调用,抛出 TypeError ,例如:

        In the case those two methods don't return a primitive, or they aren't callable, a TypeError is thrown, e.g.:

        1 == { toString:null } // TypeError!
        

        上述语句将产生 TypeError 因为默认的 Object.prototype.valueOf 方法除了实际上相同的对象实例( this )之外没有做任何其他事情,一个原始值)我们正在设置一个自己的 toString 属性,这不是一个函数。

        The above statement will produce a TypeError because the default Object.prototype.valueOf method doesn't do anything more than actually the same object instance (this, not a primitive value) and we are setting an own toString property that's not a function.

        一个朋友制作的小工具你可能会感兴趣,它显示了所有步骤和类型之间的递归比较:

        A friend made small tool that might be interesting to you, it shows all the steps and recursive comparisons made between types:

        • JS Coercion Tool

        这篇关于使用==比较整数和字符串时JavaScript中的隐式数据类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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