JavaScript:为什么布尔值的按位OR返回一个数而不是一个布尔值? [英] JavaScript: why does bitwise OR of booleans return a number instead of a boolean?

查看:141
本文介绍了JavaScript:为什么布尔值的按位OR返回一个数而不是一个布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了bools上的按位运算符在JavaScript中不会返回bool的困难方式。我认为它一定是个bug,但我在ECMAScript规范中查了一下,果然,它说按位运算符返回数字,而不是bool。它没有说明当你在布尔值上使用它们时产生的怪异。为什么这样做?我已经在其他语言中使用了这种技术多年,所以我完全不知道为什么它在JavaScript中做了不同的事情。有任何想法吗?是不是因为没有人以这种方式使用按位运算符(除了我),还是有技术原因?我无法想象检查类型并返回布尔值会很难。



作为参考,以下代码:

  var found = false; 
console.log(找到,typeof(找到));

found | = true;
console.log(找到,typeof(找到));

found = true;
console.log(找到,typeof(找到));

产生以下输出:

  false'boolean'
1'number'
true'boolean'

编辑:



根据要求,我在C,C ++中使用了这个,而且我很确定PHP,虽然我不会发誓。是的,我意识到C / C ++是打字的,所以内部会有所不同。我只是想知道为什么JavaScript会有不同的行为。



根据请求,我将如何使用一个例子| =

  var foundLowest = false; 

for(var k = 0; k< someLength; ++ k){
foundLowest | = someFunctionThatReturnsTF(k);
}

if(foundLowest === true){
/ * do stuff * /
}


解决方案

使按位运算符的行为一致(总是将其操作数转换为数字)似乎是一个充分理由使它们成为指明了他们的方式。几年前,有关添加 || = 和类似速记运营商的讨论列表的讨论,但是Eich&&  Co对于添加东西非常保守就像那样,我似乎回想起一条评论,认为它没有拉动它的语法重量。 : - )



请注意,因为JavaScript很乐意将任何值强制转换为布尔值,所以您可以愉快地使用当前样式并使其仍然有效,因为 true 强制 1 强制执行 true false 强制 0 强制执行 false 。例如:



< pre class =snippet-code-js lang-js prettyprint-override> var a = false; a | = true; //即使`a`现在是`1`,它也可以正常工作(a){snippet.log(a +=> if branch); //这是一个} else {snippet.log(a +=> else branch);} a& = false; //即使`a`现在是'0`,它也可以正常工作(a) {snippet.log(a +=> if branch);} else {snippet.log(a +=> else branch); //这一个}

 <! -  Script提供`snippet`对象,请参阅http://meta.stackexchange.com/a/242144/134069  - >< script src =http://tjcrowder.github.io/simple-snippets-console/snippet .js>< / script>  


I found out the hard way that bitwise operators on bools don't return bools in JavaScript. I thought it must be a bug, but I looked it up in the ECMAScript spec, and sure enough, it says that the bitwise operators return numbers, not bools. It doesn't say a word about the weirdness that results when you're using them on boolean values. Why is it done this way? I've used this technique for years in other languages, so I'm totally baffled why it does something different in JavaScript. Any ideas? Is it just because no one ever uses bitwise operators in this way (except me), or is there a technical reason? I can't imagine it would be hard to check the type and return a boolean.

For reference, the following code:

var found = false;
console.log(found, typeof(found));

found |= true;
console.log(found, typeof(found));

found = true;
console.log(found, typeof(found));

Produces the following output:

false 'boolean'
1 'number'
true 'boolean'

Edit:

By request, I have used this in C, C++, and I'm pretty sure PHP, although I wouldn't swear on it. Yes, I realize that C/C++ are typed, so it would be different internally. I'm just wondering why JavaScript would behave differently.

By request, an example of how I would typically use |=

var foundLowest = false;

for(var k = 0; k < someLength; ++k) {
    foundLowest |= someFunctionThatReturnsTF(k);
}

if(foundLowest === true) {
    /* do stuff */
}

解决方案

Having the bitwise operators behave consistently (always convert their operands to numbers) seems like a sufficiently good reason for them to be specified the way they are. A few years ago there was talk on the es-discuss list about adding ||= and similar shorthand operators, but Eich & Co are very conservative about adding things like that and I seem to recall a comment along the lines that it "didn't pull its syntactic weight." :-)

Note that because JavaScript is happy to coerce any value to a boolean, you can happily use your current style and have it still work, because true coerces to 1 which coerces to true, and false coerces to 0 which coerces to false. E.g.:

var a = false;

a |= true;
// Even though `a` is now `1`, it works just fine
if (a) {
  snippet.log(a + " => if branch"); // Does this one
} else {
  snippet.log(a + " => else branch");
}

a &= false;
// Even though `a` is now `0`, it works just fine
if (a) {
  snippet.log(a + " => if branch");
} else {
  snippet.log(a + " => else branch"); // Does this one
}

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

这篇关于JavaScript:为什么布尔值的按位OR返回一个数而不是一个布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆