带有单管道“|"的 Javascript 条件语句

2023-09-30前端开发问题
3

本文介绍了带有单管道“|"的 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 条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13