Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indexOf.call vs array.indexOf in jQuery's source

I was browsing the source of jQuery and bumped into this:

return indexOf.call( array, elem );

- Line 683

I was wondering what was the logic behind that, and why not do:

return array.indexOf(elem );
like image 999
marcgg Avatar asked Nov 05 '22 19:11

marcgg


1 Answers

My guess is the author of that code just does not want to care what target is passed into .inArray().

If for instance, one would call $.inArray( 42, 'hello' ) would obviously crash if we would call .indexOf() on the passed variable. The Number.prototype does (along with other types) not know about such a method.

By applying the Array.prototype.indexOf method on the passed variable, the .indexOf() method will take care of us.

like image 62
jAndy Avatar answered Nov 11 '22 04:11

jAndy