运算符| =在JavaScript中做什么? [英] What does the operator |= do in JavaScript?

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

问题描述

我在JS项目中找到了以下代码:

I found the following code in a JS project:

var a = new Array();
a[0] = 0;
for (var b = 0; b < 10; b++) {
  a[0] |= b; 
}

| = 在for循环体中做什么?

What does the |= do in the body of the for loop?

代码示例可疑,但已经提交这里由V8提供改进性能的示例。

The code example is dubious, but has been presented here by V8 for an example of improved performance.

更新示例

上述示例相当于 var a = [15]; 对于大多数意图和目的。 | = 运算符的一个更现实的例子是在单个变量中设置二进制标志,例如在权限对象上:

The above example is equivalent to var a = [15]; for most intents and purposes. A more realistic example for the |= operator would be to set up binary flags in a single variable, for example on a permission object:

//Set up permission masks
var PERMISSION_1_MASK = parseInt('0001',2);
var PERMISSION_2_MASK = parseInt('0010',2);
..

//Set up permissions
userPermissions = 0;
userPermissions |= hasPermissionOne && PERMISSION_1_MASK;
userPermissions |= hasPermissionTwo && PERMISSION_2_MASK;
..

//Use permissions
if(userPermissions & PERMISSION_1_MASK){
    ..//Do stuff only allowed by permission 1
}


推荐答案

a[0] |= b

基本上是

a[0] = a[0] | b

|是一个或按位运算符(顺便说一下: MDN文档写得非常好,非常清楚。如果OP想要编写和理解JS,那么MDN文档就是一个很好的资源。)

"|" is an or bitwise operator (by the way: The MDN docs are really well written and really clear. If the OP is wanting to write and understand JS, then the MDN docs are a great resource.)

更新
a [0] 被分配 0 时, a二进制中的[0] 0000 。在循环中,

Update When a[0] is assigned 0, a[0] in binary is 0000. In the loop,


  1. b = 0

  1. b = 0

a[0] = 0 (base 10) = 0000 (base 2)
b    = 0 (base 10) = 0000 (base 2)
                   ---------------
a[0] | b           = 0000 (base 2) = 0 (base 10)


  • b = 1

  • b = 1

    a[0] = 0 (base 10) = 0000 (base 2)
    b    = 1 (base 10) = 0001 (base 2)
                       ---------------
    a[0] | b           = 0001 (base 2) = 1 (base 10)
    


  • b = 2

  • b = 2

    a[0] = 1 (base 10) = 0001 (base 2)
    b    = 2 (base 10) = 0010 (base 2)
                       ---------------
    a[0] | b           = 0011 (base 2) = 3 (base 10)
    


  • b = 3

  • b = 3

    a[0] = 3 (base 10) = 0011 (base 2)
    b    = 3 (base 10) = 0011 (base 2)
                       ---------------
    a[0] | b           = 0011 (base 2) = 3 (base 10)
    


  • b = 4

  • b = 4

    a[0] = 3 (base 10) = 0011 (base 2)
    b    = 4 (base 10) = 0100 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    


  • b = 5

  • b = 5

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 5 (base 10) = 0101 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    


  • b = 6

  • b = 6

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 6 (base 10) = 0110 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    


  • b = 7

  • b = 7

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 7 (base 10) = 0111 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    


  • b = 8

  • b = 8

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 8 (base 10) = 1000 (base 2)
                       ---------------
    a[0] | b           = 1111 (base 2) = 15 (base 10)
    


  • b = 9

  • b = 9

    a[0] = 15 (base 10) = 1111 (base 2)
    b    =  9 (base 10) = 1001 (base 2)
                        ---------------
    a[0] | b            = 1111 (base 2) = 15 (base 10)
    


  • 在循环结束时, a [0] 的值为 15

    At the end of the loop the value of a[0] is 15

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

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