All String objects/literals have a indexOf method, if you however passed in a number or boolean etc, then you will have to convert it to String() first:
Actually I now realise that code isn't right, and having looked at the specs, the function allows an optional starting index to be used, so I re-wrote it:
Code:
Array.prototype.indexOf = Array.prototype.indexOf || function( item, idx )
{
var retVal = 0,
startIndex = ( typeof idx === 'number' ? Math.abs( Math.floor( idx ) ) : 0 );
for( var i = startIndex, len = this.length; i < len && this[ i ] !== item; i++ )
;
return i == len ? -1 : i;
}
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
Bookmarks