1. <tfoot id='62qfu'></tfoot><legend id='62qfu'><style id='62qfu'><dir id='62qfu'><q id='62qfu'></q></dir></style></legend>
      <i id='62qfu'><tr id='62qfu'><dt id='62qfu'><q id='62qfu'><span id='62qfu'><b id='62qfu'><form id='62qfu'><ins id='62qfu'></ins><ul id='62qfu'></ul><sub id='62qfu'></sub></form><legend id='62qfu'></legend><bdo id='62qfu'><pre id='62qfu'><center id='62qfu'></center></pre></bdo></b><th id='62qfu'></th></span></q></dt></tr></i><div id='62qfu'><tfoot id='62qfu'></tfoot><dl id='62qfu'><fieldset id='62qfu'></fieldset></dl></div>

      • <bdo id='62qfu'></bdo><ul id='62qfu'></ul>

      1. <small id='62qfu'></small><noframes id='62qfu'>

        为什么 {} == false 评估为 false 而 [] == false 评估为 true?

        时间:2023-09-04
      2. <tfoot id='Vshlr'></tfoot>
            <tbody id='Vshlr'></tbody>

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

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

                <legend id='Vshlr'><style id='Vshlr'><dir id='Vshlr'><q id='Vshlr'></q></dir></style></legend>
                • <bdo id='Vshlr'></bdo><ul id='Vshlr'></ul>
                  本文介绍了为什么 {} == false 评估为 false 而 [] == false 评估为 true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么 {} == false 评估为 false[] == false 评估为 true在 JavaScript 中?

                  Why does {} == false evaluate to false while [] == false evaluates to true in javascript?

                  推荐答案

                  这是根据抽象等式比较算法:

                  {} == false                // step 7 {} == ToNumber(false)
                  {} == 0                    // step 9 ToPrimitve({}) == 0
                  "[object Object]" == 0     // step 5 ToNumber("[object Object]") == 0
                  NaN == 0                   // step 1.c.i
                  
                  [] == false                // step 7 [] == ToNumber(false)
                  [] == 0                    // step 9 ToPrimitve([]) == 0
                  "" == 0                    // step 5 ToNumber("") == 0
                  0 == 0                     // step 1.c.iii
                  

                  参考:ToNumber,ToPrimitive

                  因此,更喜欢使用严格比较.

                  And because of this, prefer to use strict comparison.

                  ToPrimitive 如何在比较过程中将对象转换为基元 的一些示例.默认情况下,将调用对象的 valueOf 方法,如果 valueOf 没有返回原始值,则调用 toString.对于 Date 对象,它会默认调用 toString.

                  Some examples how ToPrimitive converts objects to primitives during comparison. By default, the valueOf method of the object will be called, and then toString if valueOf doesn't return a primitive value. For Date objects it will call toString by default.

                  var obj = {};
                  obj.valueOf();        // Object { } // the object itself
                  obj.toString();       // "[object Object]"
                  
                  
                  obj.valueOf = function() { return 123; };
                  obj == 123; // true
                  
                  obj.toString = function() { return 'foo bar'; };
                  obj == 123; // false
                  obj == 'foo bar'; // true
                  
                  // Date object
                  
                  var date = new Date();
                  date.valueOf();        // 1421430720379
                  date.toString();       // "Fri Jan 16 2015 09:52:00 GMT-0800 (PST)"
                  
                  date == 1421430720379 // false
                  date == "Fri Jan 16 2015 09:52:00 GMT-0800 (PST)" // true
                  
                  date.toString = function() { return 'foo'; };
                  date == 'foo'; // true
                  

                  这篇关于为什么 {} == false 评估为 false 而 [] == false 评估为 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:转换字符串“true"/“假"为布尔值 下一篇:javascript .filter() 真正的布尔值

                  相关文章

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

                  2. <tfoot id='8Tfw2'></tfoot>

                      <legend id='8Tfw2'><style id='8Tfw2'><dir id='8Tfw2'><q id='8Tfw2'></q></dir></style></legend>

                      <small id='8Tfw2'></small><noframes id='8Tfw2'>