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

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

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

        <tfoot id='Pqxs5'></tfoot>

          <bdo id='Pqxs5'></bdo><ul id='Pqxs5'></ul>
      1. 带有单管道“|"的 Javascript 条件语句

        Javascript conditional statement with a single pipe quot;|quot;(带有单管道“|的 Javascript 条件语句)
          <tbody id='ueBnA'></tbody>

            <tfoot id='ueBnA'></tfoot>

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

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

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

                  <i id='ueBnA'><tr id='ueBnA'><dt id='ueBnA'><q id='ueBnA'><span id='ueBnA'><b id='ueBnA'><form id='ueBnA'><ins id='ueBnA'></ins><ul id='ueBnA'></ul><sub id='ueBnA'></sub></form><legend id='ueBnA'></legend><bdo id='ueBnA'><pre id='ueBnA'><center id='ueBnA'></center></pre></bdo></b><th id='ueBnA'></th></span></q></dt></tr></i><div id='ueBnA'><tfoot id='ueBnA'></tfoot><dl id='ueBnA'><fieldset id='ueBnA'></fieldset></dl></div>
                  本文介绍了带有单管道“|"的 Javascript 条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  只是想知道以前是否有人遇到过这种情况.

                  Just wondering if anyone has come across this before.

                  我在一个项目(从另一个开发人员那里移交)中发现了一个条件语句,看起来像这样:

                  I found in a project (that was handed over from another developer) a conditional statement that looked something like this:

                  if (variableOne == true | variable2 == true) {
                      // Do something here
                  }
                  

                  它没有错误,所以似乎有效.但是,我和一位同事从未见过单管道 | 的 OR 语句,只有 2 个 ||.

                  It didn't error, so seems to work. But, myself and a colleague have never seen an OR statement with a single pipe |, only 2 ||.

                  谁能解开这个谜团?

                  谢谢,詹姆斯

                  推荐答案

                  这是一个按位或运算符.它将首先将其转换为 32 位整数,然后将按位或运算应用于结果的两个数字.在这种情况下,由于 Boolean(1) 为真且 Number(true) 为 1,因此它可以正常工作而不会出现问题(==运算符将始终返回一个布尔值,而 if 语句将任何内容转换为布尔值).以下是其工作原理的几个示例:

                  This is a bitwise OR operator. It will first convert it into a 32 bit integer, then apply the bitwise OR operation to the two numbers that result. In this instance, since Boolean(1) is true and Number(true) is 1, it will work fine without issue (the == operator will always return a boolean, and a if statement converts anything to a boolean). Here are a few examples of how it works:

                  1 | 0; // 1
                  0 | 0; // 0
                  0 | 1; // 1
                  1 | 1; // 1
                  true | false; // 1
                  false | false; // 0
                  2 | 1; // 3 (00000010, 00000001) -> (00000011)
                  

                  由于双方都必须转换为数字(并因此进行评估),因此在本应使用逻辑 OR 语句 (||) 时使用数字时,这可能会导致意外结果.为此,请举几个例子:

                  As both sides have to be converted to a number (and therefore evaluated), this may cause unexpected results when using numbers when the logical OR statement (||) was meant to be used. For this, take these examples:

                  var a = 1;
                  a | (a = 0);
                  console.log(a); // 0
                  
                  var b = 1;
                  b || (b = 0);
                  console.log(b); // 1
                  
                  // I wanted the first one
                  var c = 3 | 4; // oops, 7!
                  

                  参考:http://www.ecma-international.org/ecma-262/5.1/#sec-11.10

                  这篇关于带有单管道“|"的 Javascript 条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  <i id='3QuPF'><tr id='3QuPF'><dt id='3QuPF'><q id='3QuPF'><span id='3QuPF'><b id='3QuPF'><form id='3QuPF'><ins id='3QuPF'></ins><ul id='3QuPF'></ul><sub id='3QuPF'></sub></form><legend id='3QuPF'></legend><bdo id='3QuPF'><pre id='3QuPF'><center id='3QuPF'></center></pre></bdo></b><th id='3QuPF'></th></span></q></dt></tr></i><div id='3QuPF'><tfoot id='3QuPF'></tfoot><dl id='3QuPF'><fieldset id='3QuPF'></fieldset></dl></div>

                    • <bdo id='3QuPF'></bdo><ul id='3QuPF'></ul>
                      <tfoot id='3QuPF'></tfoot>
                    • <legend id='3QuPF'><style id='3QuPF'><dir id='3QuPF'><q id='3QuPF'></q></dir></style></legend>
                        <tbody id='3QuPF'></tbody>

                          • <small id='3QuPF'></small><noframes id='3QuPF'>