<i id='HAGvu'><tr id='HAGvu'><dt id='HAGvu'><q id='HAGvu'><span id='HAGvu'><b id='HAGvu'><form id='HAGvu'><ins id='HAGvu'></ins><ul id='HAGvu'></ul><sub id='HAGvu'></sub></form><legend id='HAGvu'></legend><bdo id='HAGvu'><pre id='HAGvu'><center id='HAGvu'></center></pre></bdo></b><th id='HAGvu'></th></span></q></dt></tr></i><div id='HAGvu'><tfoot id='HAGvu'></tfoot><dl id='HAGvu'><fieldset id='HAGvu'></fieldset></dl></div>
      • <bdo id='HAGvu'></bdo><ul id='HAGvu'></ul>

    1. <legend id='HAGvu'><style id='HAGvu'><dir id='HAGvu'><q id='HAGvu'></q></dir></style></legend>

      <tfoot id='HAGvu'></tfoot>

      1. <small id='HAGvu'></small><noframes id='HAGvu'>

        JavaScript 关系比较运算符如何强制类型?

        时间:2023-08-07

            <bdo id='nYIoO'></bdo><ul id='nYIoO'></ul>

                    <tbody id='nYIoO'></tbody>
                1. <small id='nYIoO'></small><noframes id='nYIoO'>

                  <legend id='nYIoO'><style id='nYIoO'><dir id='nYIoO'><q id='nYIoO'></q></dir></style></legend>
                2. <i id='nYIoO'><tr id='nYIoO'><dt id='nYIoO'><q id='nYIoO'><span id='nYIoO'><b id='nYIoO'><form id='nYIoO'><ins id='nYIoO'></ins><ul id='nYIoO'></ul><sub id='nYIoO'></sub></form><legend id='nYIoO'></legend><bdo id='nYIoO'><pre id='nYIoO'><center id='nYIoO'></center></pre></bdo></b><th id='nYIoO'></th></span></q></dt></tr></i><div id='nYIoO'><tfoot id='nYIoO'></tfoot><dl id='nYIoO'><fieldset id='nYIoO'></fieldset></dl></div>
                  <tfoot id='nYIoO'></tfoot>
                  本文介绍了JavaScript 关系比较运算符如何强制类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当操作数属于不同类型时,JavaScript 关系比较运算符适用哪些规则?

                  What rules apply for the JavaScript relational comparison operators when the operands are of different types?

                  例如,是怎样的>空 评估?我可以在我的开发者控制台中输入它,它会给出结果 true,但为什么呢?

                  For example, how is true > null evaluated? I can type this into my developer console and it gives the result true, but why?

                  我搜索了一下,但没有找到任何解释这一点的博客文章,尽管有很多解释 == 和 === 比较运算符的类型强制.

                  I searched for a bit, but didn't find any blog posts explaining this, although there are plenty explaining type coercion for == and === comparison operators.

                  推荐答案

                  JavaScript 关系比较运算符类型强制定义在 JavaScript 规范, 特别是在描述操作符的部分 11.8 到 11.8.5 和部分 9.1 (ToPrimitive) 和 9.3 (ToNumber)它描述了强制操作数的过程.

                  JavaScript relational comparison operator type coercion is defined in the JavaScript specification, specifically in sections 11.8 to 11.8.5 which describe the operators, and sections 9.1 (ToPrimitive) and 9.3 (ToNumber) which describe the process of coercing the operands.

                  简而言之,4 个比较运算符(<><=>=) 尽力将每个操作数转换为数字,然后比较数字.两个操作数都是字符串时例外,在这种情况下,它们按字母顺序进行比较.

                  In short, the 4 comparison operators (<, >, <=, and >=) do their best to convert each operand to a number, then compare the numbers. The exception is when both operands are strings, in which case they are compared alphabetically.

                  具体来说,

                  1. 如果参数 o 是对象而不是原语,尝试转换它通过调用 o.valueOf() 或 - 如果 o.valueOf 未定义或调用时未返回原始类型 -通过调用 o.toString()

                  1. If an argument o is an object instead of a primitive, try to convert it to a primitive value by calling o.valueOf() or - if o.valueOf wasn't defined or didn't return a primitive type when called - by calling o.toString()

                  如果两个参数都是字符串,请根据它们的字典顺序比较它们.例如,这意味着 "a" <"b""a" <"aa" 都返回 true.

                  If both arguments are Strings, compare them according to their lexicographical ordering. For example, this means "a" < "b" and "a" < "aa" both return true.

                  否则,将每个原语转换为数字,这意味着:

                  • 未定义 -> NaN
                  • -> +0
                  • Boolean 原始类型 -> 1 if true, +0 if false
                  • String -> 尝试从字符串中解析一个数字
                  • undefined -> NaN
                  • Null -> +0
                  • Boolean primitive type -> 1 if true, +0 if false
                  • String -> try to parse a number from the string

                  然后按照您对运算符的期望比较每个项目,但需要注意的是,任何涉及 NaN 的比较都会评估为 false.

                  Then compare each item as you'd expect for the operator, with the caveat that any comparison involving NaN evaluates to false.

                  所以,这意味着以下内容:

                  So, this means the following:

                  console.log(true > null);           //prints true
                  console.log(true > false);          //prints true
                  console.log("1000.0" > 999);        //prints true
                  console.log("  1000	
                  " < 1001);   //prints true
                  
                  var oVal1 = { valueOf: function() { return 1; } };
                  var oVal0 = { toString: function() { return "0"; } };
                  
                  console.log(oVal1 > null);         //prints true
                  console.log(oVal0 < true);         //prints true
                  console.log(oVal0 < oVal1);        //prints true
                  

                  这篇关于JavaScript 关系比较运算符如何强制类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 JavaScript 中将 Object {} 转换为 Array [] 键值 下一篇:JavaScript 中是 true == 1 和 false == 0 吗?

                  相关文章

                  最新文章

                3. <small id='Sa8cz'></small><noframes id='Sa8cz'>

                    1. <legend id='Sa8cz'><style id='Sa8cz'><dir id='Sa8cz'><q id='Sa8cz'></q></dir></style></legend>

                      • <bdo id='Sa8cz'></bdo><ul id='Sa8cz'></ul>
                    2. <tfoot id='Sa8cz'></tfoot>
                      <i id='Sa8cz'><tr id='Sa8cz'><dt id='Sa8cz'><q id='Sa8cz'><span id='Sa8cz'><b id='Sa8cz'><form id='Sa8cz'><ins id='Sa8cz'></ins><ul id='Sa8cz'></ul><sub id='Sa8cz'></sub></form><legend id='Sa8cz'></legend><bdo id='Sa8cz'><pre id='Sa8cz'><center id='Sa8cz'></center></pre></bdo></b><th id='Sa8cz'></th></span></q></dt></tr></i><div id='Sa8cz'><tfoot id='Sa8cz'></tfoot><dl id='Sa8cz'><fieldset id='Sa8cz'></fieldset></dl></div>