JavaScript中布尔运算的幂运算符? [英] Exponentiation operator for Boolean in JavaScript?

查看:147
本文介绍了JavaScript中布尔运算的幂运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅exponentiation operator返回将第一个操作数加到幂第二个操作数的结果,就像Python中的幂运算符一样,这​​是ECMAScript 2016(ES7)提议的一部分.

Refer to this, the exponentiation operator returns the result of raising first operand to the power second operand, like the exponentiation operator in Python, which is part of the ECMAScript 2016 (ES7) proposal.

我们知道Booleanexponentiation operator在Python中的结果如下:

We know the result of Boolean with exponentiation operator in Python as following:

>>> False ** False == True
True
>>> False ** True == False
True
>>> True ** False == True
True
>>> True ** True == True
True

我想知道Boolean是否可以在exponentiation operator中使用?如果是这样,是否可能与Python中的行为相同?

I want to know whether the Boolean could be used in the exponentiation operator? If so, could the same behavior as above in Python?

推荐答案

我不确定您期望什么样的答案.如果您查看提案,您将请注意,两个操作数都首先转换为数字.这意味着false ** false等同于0 ** 0.

I'm not sure what kind of answer you expect. If you look at proposal you will notice that both operands are converted to numbers first. That means false ** false is equivalent to 0 ** 0.

是的,您可以将运算符应用于布尔值.就像所有其他运算符一样,这​​些值将转换为该运算符期望的类型.

So yes, you can apply the operator to Booleans. Just like with all the other operators, the values are converted to the type that the operator expects.

结果将始终是数字.

但是,如果您使用松散比较,那么,如果求幂的结果是1,它将宽松地等于true,如果是0,它将宽松等于false.

However, of course if you use loose comparison, then if the result of the exponentiation is 1, it will loosely equal true, if it is 0, it will loosely equal false.

这篇关于JavaScript中布尔运算的幂运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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