5

Possible Duplicate:
What good does zero-fill bit-shifting by 0 do? (a >>> 0)

I was looking at array.indexOf(), and I'm aware that IE7 does not natively support that. I was reading the MDC and saw their example of how to prototype it into browsers that don't support it. I'm reading through it trying to understand how everything works, but I'm not sure I'm understanding it 100%. The main cause for confusion is the bitwise operators, specifically >>>. I'm not sure what this operator is useful for. Below is the way that they're using it. Can anyone explain exactly what it's useful for and why you can't just if (t.length === 0)?

 var t = Object(this);  
 var len = t.length >>> 0;  
 if (len === 0)  
   return -1; 
Community
  • 1
  • 1
A Wizard Did It
  • 3,614
  • 4
  • 28
  • 32
  • I read that too just now, and I'm still not 100%. His explanation seems to say it ensures it is a non negative number... but like one of the comments, I wasn't aware array.length could return a negative number. – A Wizard Did It Nov 11 '10 at 03:36
  • read my comment to @Marcel in [my answer](http://stackoverflow.com/questions/3081987/#3082073), the methods of `Array.prototype` are **intentionally generic** that means that they can be used on Array-like objects. Moreover, the Mozilla implementation of those Array methods, tries to mimic the behavior of the [ECMA-262 Specification](http://ecma262-5.com/ELS5_HTML.htm#Section_15.4.4.14) as best as possible, and the `>>>` operator is the shortest way to mimic the [`ToUint32` internal operation](http://ecma262-5.com/ELS5_HTML.htm#Section_9.6), used internally by all those methods... – Christian C. Salvadó Nov 11 '10 at 04:26

1 Answers1

7

It allows indexOf to be called on array-like objects that might have weird length properties.

For example:

var fakeArray = { length: -3, '0': true, '1': false, '2': null };
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I'm sorry, what's an example of an 'array-like' object? – A Wizard Did It Nov 11 '10 at 03:37
  • @Stanley: Any object with a `length` proeprty and properties named `0`, `1`, `2`, ... – SLaks Nov 11 '10 at 03:41
  • Oh, your edit really cleared things up... I didn't even think of it like that. Thanks. – A Wizard Did It Nov 11 '10 at 03:43
  • @Stanley: What you get out of doing `document.getElementsByTagName('span')`. That's a NodeList, which acts like an array but isn't. There are others in javascriptland but that's the most common. Though, I'm not aware of any array-like object where length can be negative. – slebetman Nov 11 '10 at 03:45
  • @slebetman, Note that with [host objects](http://ecma262-5.com/ELS5_HTML.htm#Section_4.3.8) -such NodeLists- [the specification](http://ecma262-5.com/ELS5_HTML.htm#Section_15.4.4.14) doesn't guarantees that Array methods such `indexOf`, `slice`, etc, can be applied to them: "Whether the indexOf function can be applied successfully to a host object is implementation-dependent." – Christian C. Salvadó Nov 11 '10 at 04:31
  • @CMS: I was answering to `what's an example of an 'array-like' object?`. – slebetman Nov 11 '10 at 11:52