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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With