We already went thru this. I actually backed up agreed with you back
then.
It is not THAT inconsistent though. jQuery utility methods ($.each,
$.map, $.filter) pass "element, index". That's the logical order.
Now, jQuery's prototype methods make the 'this' point to the actual
element. It is commonly used by everyone and few people actually care
about the 2nd argument (the element). That's why they are reordered,
you can already access the element using 'this' and that makes the
code more readable and intuitive for most users.

Cheers

--
Ariel Flesler
http://flesler.blogspot.com

On Aug 20, 1:36 am, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> And one heck of an inconsistency, don't you think?
>
> jQuery.each() and jQuery.map() don't pass the arguments to their callbacks
> in the same order?! That's definitely a mistake.
>
> Actually, the original order chosen for the arguments to the $(...).each()
> and $.each() callbacks was unfortunate. callback(index,element) is
> backwards. It's rare that you need the array index; what you want in almost
> every situation is the array *element*.
>
> The (new) native JavaScript [].forEach() and [].map() methods got it right:
> their callbacks receive the arguments in this order:
> callback(element,index,array).
>
> The reason we ended up with callback(index,element) was simply that the
> original jQuery code didn't have either of those arguments. It was simply
> callback() with no arguments, and the array element in "this". The index
> argument got added later, and the element argument was added after that. The
> order of arguments is simply a historical accident: it's the chronological
> order in which the arguments were added to the code!
>
> Why does it matter? Consider the following array:
>
>     var array = [
>         [ 'a', 'b' ],
>         [ 'c', 'd' ]
>     ];
>
> Now iterate over its elements (in both dimensions) using .forEach() and
> $.each():
>
>     array.forEach( function( outer ) {
>         outer.forEach( function( inner ) {
>             console.log( inner );
>         });
>     });
>
>     $.each( array, function( i, outer ) {
>         $.each( outer, function( j, inner ) {
>             console.log( inner );
>         });
>     });
>
> The $.each() version requires placeholder "i" and "j" arguments which the
> .forEach() version does not require.
>
> It's a small thing, but if you want to (wisely!) write jQuery quote that
> uses explicit callback arguments instead of overusing "this", you have to
> throw in those extra arguments every time.
>
> Moot point now, though, it's a done deal.
>
> And given that it's a done deal, $.map() should have been consistent with
> the other jQuery callbacks.
>
> Oops... :-)
>
> -Mike
>
> > From: Ariel Flesler
>
> > You have both jQuery.fn.map
> >    http://docs.jquery.com/Traversing/map
>
> > And jQuery.map
> >    http://docs.jquery.com/Utilities/jQuery.map
>
> > Both documented correctly.
> > On Aug 19, 5:51 pm, moester <[EMAIL PROTECTED]> wrote:
> > > The argument order of the $.map  callback function seems to be
> > > reversed depending on how $.map is used.
>
> > > >>> arr = ['a','b','c']
>
> > > ["a", "b", "c"]
>
> > > >>> $.map(arr,function(n,i){return [n,i]})
>
> > > ["a", 0, "b", 1, "c", 2]                                    <--
> > > according to docs
>
> > > >>> $(arr).map(function(n,i){return [n,i]}).get()
>
> > > [0, "a", 1, "b", 2, "c"]                                    <--
> > > reverse of docs!!
>
> > > I realize in the second case one could use 'this' to get
> > > the value and just use one argument for the index,
> > > which is more consistent with other chainable functions.
> > > Perhaps this could be made clear in the docs or with an
> > > example.

Reply via email to