JavaScript 三重大于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7718711/
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
JavaScript triple greater than
提问by Jey Balachandran
I saw this syntax on another StackOverflow postand was curious as to what it does:
我在另一个StackOverflow 帖子上看到了这种语法,并且很好奇它的作用:
var len = this.length >>> 0;
var len = this.length >>> 0;
What does >>>
imply?
是什么>>>
暗示?
采纳答案by Joe
That's an unsigned right shift operator. Interestingly, it is the only bitwise operatorthat is unsignedin JavaScript.
那是一个无符号的右移运算符。有趣的是,它是唯一位运算符是无符号在JavaScript中。
The >>> operator shifts the bits of expression1 right by the number of bits specified in expression2. Zeroes are filled in from the left. Digits shifted off the right are discarded.
>>> 运算符将 expression1 的位右移 expression2 中指定的位数。从左边开始填零。丢弃右移的数字。
回答by Eric
Ignoring its intended meaning, this is most likely where you'll see it used:
忽略其预期含义,这很可能是您看到它使用的地方:
>>> 0
is unique in that it is the only operator that will convert any type to a positive integer:
>>> 0
它的独特之处在于它是唯一将任何类型转换为正整数的运算符:
"string" >>> 0 == 0
(function() { }) >>> 0 == 0
[1, 2, 3] >>> 0 == 0
Math.PI >>> 0 == 3
In your example, var len = this.length >>> 0
, this is a way of getting an integer length to use to iterate over this
, whatever type this.length
may be.
在您的示例中var len = this.length >>> 0
,这是一种获取整数长度以用于迭代的方法this
,无论类型this.length
是什么。
Similarly, ~~x
can be used to convert any variable into a signed integer.
同样,~~x
可用于将任何变量转换为有符号整数。
回答by Mark Byers
That operator is a logical right shift. Here the number is shifted 0 bits. A shift of zero bits mathemetically should have no effect.
该运算符是逻辑右移。这里的数字被移动了 0 位。数学上的零位移位应该没有影响。
But here it is used to convert the value to an unsigned 32 bit integer.
但在这里它用于将值转换为无符号的 32 位整数。
回答by nrabinowitz
>>>
is a bit-wise operator, zero-fill right shift.
>>>
是一个按位运算符,零填充右移。
I think the only effect of >>> 0
on a positive number is to round down to the nearest integer, same as Math.floor()
. I don't see why this would be necessary in your example, as generally a .length
property (e.g. of an Array
) would be an integer already.
我认为>>> 0
对正数的唯一影响是向下舍入到最接近的整数,与Math.floor()
. 我不明白为什么这在您的示例中是必要的,因为通常.length
属性(例如 an Array
)已经是一个整数。
I've also seen the slightly shorter ~~
used in the same way: ~~9.5 == 9; // true
.
我还看到了~~
以相同方式使用的稍短的:~~9.5 == 9; // true
.