JavaScript 中的单个竖线是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4024429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does a single vertical bar mean in JavaScript?
提问by samrockon
What does this expression mean in JS?
这个表达式在JS中是什么意思?
Value |= this.value
采纳答案by Pointy
It's binary "OR", just like in C or C++ or Java. In this case, it's used in its assignment operator form, so
它是二进制“或”,就像在 C 或 C++ 或 Java 中一样。在这种情况下,它以其赋值运算符的形式使用,所以
value |= this.value
means that this.valueand valueare both converted to 32-bit integers, and a bitwise OR operation is performed. If valuewere 10 and this.valuewere 3 before the operation (that is, 01010and 011in binary) the result would be 11 (01011in binary).
意味着this.value和value都转换为 32 位整数,并执行按位或运算。如果value为10和this.value操作之前分别为3(即,01010与011在二进制)的结果将是11(01011二进制)。
The binary logic operators in Javascript are notable in Javascript because the work is carried out on integervalues.
Javascript 中的二元逻辑运算符在 Javascript 中值得注意,因为该工作是在整数值上执行的。
The term "bit-wise" is perhaps more accurate than "binary". The operations act on each bit of a numeric value, specifically the numeric values coerced to signed 32-bit integers. The result is also a signed 32-bit integer (according to the spec).
术语“按位”可能比“二进制”更准确。这些操作作用于数值的每一位,特别是强制转换为有符号 32 位整数的数值。结果也是一个有符号的 32 位整数(根据规范)。
However, JavaScript numbers "at rest" are always 64-bit binary floating point values. Thus the results of bitwise operators, though computed with 32-bit integer math, are stored in floating point form. That works because the range of 32-bit integers fits comfortably and precisely in a 64-bit float.
但是,“静止”的 JavaScript 数字始终是 64 位二进制浮点值。因此,按位运算符的结果虽然使用 32 位整数数学计算,但以浮点形式存储。之所以有效,是因为 32 位整数的范围恰好适合 64 位浮点数。
回答by Frédéric Hamidi
This will perform a bitwise ORbetween the bits in this.valueand the bits already stored in Value, then store the result back into Value.
这将在 中的位和已经存储在 中的位之间执行按位或运算,然后将结果存回 中。this.valueValueValue
var Value = 42; // 00101010
Value |= 96; // 01100000
window.alert(Value); // 01101010 -> 106
回答by JulianR
As others have pointed out, this is the bitwise OR operator. However, I don't think people use it much on numerical values in Javascript as - generally - you don't do a lot of computation in Javascript. To give you a better idea why this operator is useful, consider the far more common scenario that the user needs to fill in at least one of multiple textfields.
正如其他人指出的那样,这是按位 OR 运算符。但是,我不认为人们在 Javascript 中对数值使用它太多 - 通常 - 您不会在 Javascript 中进行大量计算。为了让您更好地了解此运算符为何有用,请考虑更常见的场景,即用户需要填写多个文本字段中的至少一个。
Say you have this HTML:
假设你有这个 HTML:
<input type="text" class="phone-nr" id="home-phone-nr-1" />
<input type="text" class="phone-nr" id="home-phone-nr-2" />
<input type="text" class="phone-nr" id="home-phone-nr-3" />
<input type="text" class="phone-nr" id="mobile-phone-nr-1" />
<input type="text" class="phone-nr" id="mobile-phone-nr-2" />
<input type="text" class="phone-nr" id="mobile-phone-nr-3" />
The user has the option to fill in multiple phone numbers, but will have to supply at least one.
用户可以选择填写多个电话号码,但必须至少提供一个。
The easiest way to do this (with jQuery in this case) is:
最简单的方法(在这种情况下使用 jQuery)是:
var valid = false;
$('.phone-nr').each(function(i, item){
valid |= $(item).val();
}); // untested code
validwill be true if at least one input field with class phone-nrhas a non-empty value.
valid如果至少一个具有类的输入字段具有phone-nr非空值,则为真。
If every field mustbe filled in (a more common requirement) you can do it like this with the bitwise AND operator:
如果必须填写每个字段(更常见的要求),您可以使用按位 AND 运算符这样做:
var valid = true;
$('.phone-nr').each(function(i, item){
valid &= $(item).val();
}); // untested code
validwill only be true if allinput fields have a value.
valid仅当所有输入字段都有值时才会为真。
If at least single field is required to be filled in, but no more than oneyou can use the XOR operator:
如果至少需要填写一个字段,但不能超过一个,则可以使用 XOR 运算符:
var valid = false;
$('.phone-nr').each(function(i, item){
valid ^= $(item).val();
}); // untested code
Those are, in my opinion, the real-world uses of bitwise operators in Javascript.
在我看来,这些是 JavaScript 中按位运算符的实际用途。
回答by Dan
Practical use for the operator
操作员的实际使用
( 3|0 ) === 3;
( 3.3|0 ) === 3;
( 3.8|0 ) === 3;
( -3.3|0 ) === -3;
( -3.8|0 ) === -3;
( "3"|0 ) === 3;
( "3.8"|0 ) === 3;
( "-3.8"|0 ) === -3;
( NaN|0 ) === 0;
( Infinity|0 ) === 0;
( -Infinity|0 ) === 0;
( null|0 ) === 0;
( (void 0)|0 ) === 0;
( []|0 ) === 0;
( [3]|0 ) === 3;
( [-3.8]|0 ) === -3;
( [" -3.8 "]|0 ) === -3;
( [-3.8, 22]|0 ) === 0
( {}|0 ) === 0;
( {'2':'3'}|0 ) === 0;
( (function(){})|0 ) === 0;
( (function(){ return 3;})|0 ) === 0;
Some magic for me
给我一些魔法
3 | '0px' === 3;
回答by Kendrick
It's a bitwise or assignment operator, similar to +=. If you run a test on it like this:
它是一个按位或赋值运算符,类似于+=. 如果您像这样对其进行测试:
<ol>
<script language="javascript">
var x=false;
document.writeln("<li>"+x+"</li>");
x|=true;
document.writeln("<li>"+x+"</li>");
x&=false;
document.writeln("<li>"+x+"</li>");
</script>
</ol>
You'll get this output (in IE)
你会得到这个输出(在 IE 中)
1.false
2.1
3.0
Essentially, x|=yis the same as saying x=x|y
本质上, x|=y和说一样x=x|y

