• <tfoot id='HIPQP'></tfoot>

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

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

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

        何时在 JavaScript 中使用双非 (!!) 运算符

        When to use the double not (!!) operator in JavaScript(何时在 JavaScript 中使用双非 (!!) 运算符)

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

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

        • <legend id='AiL4p'><style id='AiL4p'><dir id='AiL4p'><q id='AiL4p'></q></dir></style></legend>

          <tfoot id='AiL4p'></tfoot>

              <tbody id='AiL4p'></tbody>
            • <bdo id='AiL4p'></bdo><ul id='AiL4p'></ul>
                  本文介绍了何时在 JavaScript 中使用双非 (!!) 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我了解 双非运算符的作用JavaScript.我很好奇它的用途以及我最近的断言是否正确.

                  I understand what the double not operator does in JavaScript. I'm curious about it's use though and whether or not a recent assertion that I made is correct.

                  我说过 if (!!someVar) 从来没有意义,(!!someVar && ... 也没有意义,因为 if&& 将导致 someVar 被评估为布尔值,因此 !! 是多余的.

                  I said that if (!!someVar) is never meaningful nor is (!!someVar && ... because both the if and the && will cause someVar to be evaluated as a boolean so the !! is superfluous.

                  事实上,我唯一能想到使用 double not 运算符是合法的情况是,如果您想与另一个布尔值进行严格比较(因此可能在返回值中明确地期望 true 或 false).

                  In fact, the only time that I could think of that it would be legitimate to use the double not operator is if you wanted to do a strict comparison to another boolean value (so maybe in return value that expects true or false explicitly).

                  这是正确的吗?当我注意到 jQuery 1.3.2 同时使用了 if (!!someVar)return !!someVar && 时,我开始怀疑自己....

                  Is this correct? I started to doubt myself when I noticed jQuery 1.3.2 used both if (!!someVar) and return !!someVar && ...

                  double 在这些情况下没有任何实际效果吗?

                  Does the double not have any actual effect in these situations?

                  我个人的看法是,它只会导致混乱.如果我看到一个 if 语句,我知道它会将其评估为布尔值.

                  My personal opinion is that it just leads to confusion. If I see an if statement, I know it's evaluating it as a boolean.

                  推荐答案

                  if 语句 我和你在一起,它是完全安全的,因为在内部,ToBoolean 操作将在条件表达式上执行(参见步骤3 关于规范).

                  In the context of if statements I'm with you, it is completely safe because internally, the ToBoolean operation will be executed on the condition expression (see Step 3 on the spec).

                  但是如果你想,比如说,从一个函数返回一个布尔值,你应该确保结果实际上是布尔值,例如:

                  But if you want to, lets say, return a boolean value from a function, you should ensure that the result will be actually boolean, for example:

                  function isFoo () {
                    return 0 && true;
                  }
                  
                  console.log(isFoo()); // will show zero
                  typeof isFoo() == "number";
                  

                  总之,布尔逻辑运算符 可以返回一个操作数,而不一定是 Boolean 结果:

                  In conclusion, the Boolean Logical Operators can return an operand, and not a Boolean result necessarily:

                  逻辑与运算符 (&&) 将返回 第二个操作数 的值,如果第一个是真正:

                  The Logical AND operator (&&), will return the value of the second operand if the first is truly:

                  true && "foo"; // "foo"
                  

                  如果它本身falsy,它将返回第一个操作数的值:

                  And it will return the value of the first operand if it is by itself falsy:

                  NaN && "anything"; // NaN
                  0 && "anything"; // 0
                  

                  另一方面,逻辑或运算符 (||) 将返回 第二个操作数 的值,如果第一个是 falsy:

                  On the other hand, the Logical OR operator (||) will return the value of the second operand, if the first one is falsy:

                  false || "bar"; // "bar"
                  

                  如果它本身是非假的,它将返回第一个操作数的值:

                  And it will return the value of the first operand if it is by itself non-falsy:

                  "foo" || "anything"; // "foo"
                  

                  也许值得一提的是,falsy 值是:nullundefinedNaN0,零长度字符串,当然还有 false.

                  Maybe it's worth mentioning that the falsy values are: null, undefined, NaN, 0, zero-length string, and of course false.

                  布尔上下文,将返回 true.

                  这篇关于何时在 JavaScript 中使用双非 (!!) 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

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

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

                              <tbody id='YbgQO'></tbody>

                          • <tfoot id='YbgQO'></tfoot>
                          • <legend id='YbgQO'><style id='YbgQO'><dir id='YbgQO'><q id='YbgQO'></q></dir></style></legend>