<tfoot id='z1m76'></tfoot>

      <legend id='z1m76'><style id='z1m76'><dir id='z1m76'><q id='z1m76'></q></dir></style></legend>

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

        <small id='z1m76'></small><noframes id='z1m76'>

        JavaScript IF 语句 boolean strict

        时间:2023-09-04
            <tbody id='D9K7q'></tbody>
        1. <small id='D9K7q'></small><noframes id='D9K7q'>

          <legend id='D9K7q'><style id='D9K7q'><dir id='D9K7q'><q id='D9K7q'></q></dir></style></legend>
            <bdo id='D9K7q'></bdo><ul id='D9K7q'></ul>

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

                  本文介绍了JavaScript IF 语句 boolean strict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我通过将真实"布尔值传递给函数来在脚本中设置模式.在函数中,一个简单的 if 语句检查参数是 true 还是 false.我有时将它作为一直有效的字符串或布尔值传递,但由于某种原因它不是为此:

                  I am setting a mode in a script by passing a "truthy" boolean to a function. In the function a simple if statement checks if the param is true or false. I sometimes pass it as a string or a bool which has always worked, but for some reason it isn't for this:

                  setMode: function(Setting) {    
                      if (Setting) {
                          console.log('Enabling mode');
                      } else {
                          console.log('Disabling mode');
                      }
                  }
                  

                  例如,当我向它传递一个字符串 'false' 并将 Setting 记录到控制台时,控制台说 false,但 >if 语句认为是真的.

                  For example when I pass it a string 'false' and log Setting to console, the console says false, yet the if statement thinks it's true.

                  我必须将其添加到函数的开头才能使其工作:

                  I have to add this to the start of the function for it to work:

                  if (typeof Setting == 'string') {设置=(设置==真");}

                  if (typeof Setting == 'string') { Setting = (Setting == "true"); }

                  使用示例:

                  var inverted = $('INPUT[name="Mode"]').prop('checked');
                  app.setMode(inverted);
                  

                  还有一个:

                  var mode = localStorage.getItem('Mode');
                  this.setMode(mode);
                  

                  这太奇怪了,因为我已经做了很多年了,但现在才刚刚开始.可能是因为我使用的是 localStorage.prop?

                  It's so bizarre since I've done this type of thing for years yet it's only starting now. Maybe because I'm using localStorage and .prop?

                  推荐答案

                  回答关于字符串到布尔转换的问题:

                  To answer your question about string to boolean conversions:

                  ECMA 规范:

                  如果参数是空字符串(其长度为零),则[此转换的]结果为假;否则结果为真.

                  The result [of this conversion] is false if the argument is the empty String (its length is zero); otherwise the result is true.

                  所以是的,如果您传递字符串false",它将被转换为 true,这意味着您唯一的选择是手动检查字符串true"或false"并且自己做转换.

                  So yes, if you pass the string "false" it will be converted to true, which means the only option you have is to manually check for the strings "true" or "false" and do the conversion by yourself.

                  然而,jQuery 函数 .prop("checked") 应该返回一个布尔值(如果属性未定义,则返回 undefined).所以我会说你应该能够真正做到

                  However, the jQuery function .prop("checked") is supposed to return a boolean value (or undefined if the property is not defined). So I would say you should be able to actually do

                  if (elem.prop("checked"))
                  

                  喜欢这里.

                  这篇关于JavaScript IF 语句 boolean strict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Javascript 中比较字符串和布尔值? 下一篇:OO Javascript 是否有 `to_bool` 或 `__len__` 等价物?

                  相关文章

                    <bdo id='1EHAI'></bdo><ul id='1EHAI'></ul>

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

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

                      <tfoot id='1EHAI'></tfoot>