The case is precisely it: We do it that way because we need to support
array-like objects as well. (The docs are written in that particular
manner because saying .inArray(ArrayLikeObject) would probably just
confuse the issue.)

If the difference is between doing an extra .call() (which is slower
than a regular function call) and doing one extra property lookup then
the property lookup seems just as reasonable.

Up to you, though. If a bug is filed and some code landed on a Github
branch (that works with non-arrays) then I'll probably just merge it
in.

--John



On Mon, Dec 14, 2009 at 12:21 PM, Markw65 <mark...@gmail.com> wrote:
> Why is it done this way:
>
>       inArray: function( elem, array ) {
>                if ( array.indexOf ) {
>                        return array.indexOf( elem );
>                }
>
>                for ( var i = 0, length = array.length; i < length; i++ ) {
>                        if ( array[ i ] === elem ) {
>                                return i;
>                        }
>                }
>
>                return -1;
>        },
>
> rather than:
>      inArray: Array.prototype.indexOf ? function(elem,array) { return
> array.indexOf(elem) } :
>           function(elem,array) { ... },
>
> ie, why do the test every time inArray is called, rather than once,
> when the library is loaded?
>
> The only reason I can think of is that inArray is supposed to work on
> "array-like" objects too. But the docs explicitly say "Array" for the
> type of the argument...
>
> Even if that case does need to be supported, presumably something like
> "return Array.prototype.indexOf.call(array, elem)" would do the trick?
>
> Mark
>
> On Dec 14, 7:57 am, Brandon Aaron <brandon.aa...@gmail.com> wrote:
>> http://github.com/jquery/jquery/blob/master/src/core.js#L49http://github.com/jquery/jquery/blob/master/src/core.js#L571
>>
>> --
>> Brandon Aaron
>>
>>
>>
>> On Mon, Dec 14, 2009 at 4:16 AM, caii <caimeic...@beyondsoft.com> wrote:
>> > Array.prototype.indexOf?
>> > where it in JQUERY?
>>
>> > On Dec 14, 6:13 pm, Robert Katić <robert.ka...@gmail.com> wrote:
>> >> Already fixed. If Array.prototype.indexOf exists, then it is always
>> >> used.
>>
>> >> On Dec 14, 10:38 am, helianthus <project.heliant...@gmail.com> wrote:
>>
>> >> > This occurs when using jQuery.fn.index, which utilizes jQuery.inArray.
>> >> > Since the jQuery object does not have an indexOf method, it falls back
>> >> > to the for loop approach, while Array.prototype.indexOf could have
>> >> > been used.
>>
>> > --
>>
>> > You received this message because you are subscribed to the Google Groups 
>> > "jQuery Development" group.
>> > To post to this group, send email to jquery-...@googlegroups.com.
>> > To unsubscribe from this group, send email to 
>> > jquery-dev+unsubscr...@googlegroups.com.
>> > For more options, visit this group 
>> > athttp://groups.google.com/group/jquery-dev?hl=en.
>
> --
>
> You received this message because you are subscribed to the Google Groups 
> "jQuery Development" group.
> To post to this group, send email to jquery-...@googlegroups.com.
> To unsubscribe from this group, send email to 
> jquery-dev+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/jquery-dev?hl=en.
>
>
>

--

You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-...@googlegroups.com.
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en.


Reply via email to