【发布时间】:2015-10-19 23:37:31
【问题描述】:
从我的评论中很容易看出我在函数开始时尝试做的事情
this.move = function ( )
{
if (this.dx | this.dy != 0) return; // exit early if both this.dx and this.dy are zero
也就是说,我想要if (this.dx == 0 && this.dy == 0) 的等价物。我认为按位OR 是正确的,因为this.dx | this.dy 不等于zero 当且仅当this.dx 至少有一位或this.dy 至少有一位(或两者都至少有一位)一点点)。但我一定是错的,因为我的测试
this.move = function ( )
{
console.log("this.dx = " + this.dx + ", this.dy = " + this.dy); // TEST
if (this.dx | this.dy != 0) return; // exit early if both this.dx and this.dy are zero
表示当this.dx 和this.dy 都为零时,函数的其余部分正在执行。
这里发生了什么?
【问题讨论】:
-
当您的意思是
==时,您使用的是!=。如果它们 都是 都为零,则您想要返回,因此如果this.dx | this.dy等于 0。musicly_ut 还指出了优先级错误。我个人会写if (this.dx == 0 && this.dy == 0),因为它更容易阅读......
标签: javascript optimization binary bit-manipulation bitwise-operators