Tuesday 15 June 2010

What is the JavaScript >>> operator and how do you use it? -


I was looking at code from Mozilla that adds a filter method to the array and there was a line of code that I Was confused.

  var len = this.length & gt; & Gt; & Gt; 0;  

I have never seen >>> Used earlier in JavaScript.
What is it and what does it do?

This does not change only non-numbers, according to the number, it converts them into those numbers Which can be expressed as 32-bit unsigned ints.

Although JavaScript's number is double precision floats (*), the bitwair operator ( , & gt; , < Code> & amp; , | and ~ ) are defined in terms of operating 32-bit signed Entities by performing bitward operations on 32-bit integers The number changes, before calculating and then converting to the number, any fraction and high position bits are lost compared to 32 digits.

0 bits & gt; & Gt; Any real effect, like the shift from 0 to the right of 0 , is a quick way to round the number and to ensure that it is in the 32-bit int range, in addition, triple & gt; & Gt; & Gt; Operator, after its unsigned operation, converts the number of results into an unsigned integer instead of the signed integer, so it can be 32-bit-two as a large number. The version of the supplement is used to convert negative & gt; & Gt; & Gt; Using 0 ensures that you have found an integer between 0 and 0xFFFFFFFF.

In this situation it is useful because ECMAScript defines array indexes in reference to 32 bit unsigned ints, so if you try to implement array.filter in such a manner If the ECMAScript denotes the fifth edition standard, then you will enter this number like 32-bit unsigned int like this.

(Actually there is very little practical requirement for this because people array.length to 0.5 , -1 settings Not going,> 1e21 or 'LEMONS' . But we are talking about this JavaScript authors, you never know ...) < / P>

summary:

  1 >>> === 1 -1 >> gt; & Gt; 0 === 0xFFFFFFFF -1 & gt; 0 === -1 1.7 & gt; & Gt; & Gt; 0 === 1 0x100000002> gt0 === 2 1e21> gt0 === 0xDEA00000 1e21> 0 === -0x21600000 Infinity & gt; & Gt; & Gt; 0 === 0 Nan & gt; & Gt; 0 === 0 Tap & gt; & Gt; 0 === 0 '1'> gt; & Gt; 0 === 1 'x' & gt; & Gt; & Gt; 0 === 0 Object & gt; ; & Gt; & Gt; 0 === 0  

(*: Okay, they're d like behaving like float, it would not surprise me that some javascript engines actually use the inputs used , Because for performance reasons but it will be an implementation description that you will not be able to take advantage of it.)


No comments:

Post a Comment