Sorry, I didn't mean to sound like I was bringing up an old argument. Like I
said, the argument order to the .each() callback was a done deal, about two
years ago. I brought it up mostly to give some historical context to the
confusing situation we have now.

If jQuery's $().each(), $().map(), and related functions were being designed
today, it would be a no-brainer: simply follow the standard set by the
native JavaScript [].forEach() and [].map() functions.

I'm sure every API designer has one or two APIs that they wish they could go
back and change. Positional arguments make this worse: If you add them over
time as you need them in various use cases, then the final result may not be
what you would do if you had a chance to design the function call with all
the use cases in mind.

Back to the present, I'm afraid the situation with $.each and $.map is worse
than you realize. They are not consistent with each other!

Paste this code into the multiline Firebug console and you can see. (In case
anyone wonders, the unconventional use of map and grep is deliberate, to
make the test cases consistent and make sure we're seeing only the argument
order and not any other factors.)

// begin test code

var a = [ 'a', 'b', 'c' ];

var e = [];
$.each( a, function() {
    e.push( arguments );
});

var g = [];
$.grep( a, function() {
    g.push( arguments );
});

var m = [];
$.map( a, function() {
    m.push( arguments );
});

console.log( '$.each:', e );
console.log( '$.grep:', g );
console.log( '$.map: ', m );

// end test code

The result is:

$.each: [[0, "a"], [1, "b"], [2, "c"]]
$.grep: [["a", 0], ["b", 1], ["c", 2]]
$.map: [["a", 0], ["b", 1], ["c", 2]]

Ouch!
 
It's probably to late to fix this inconsistency too. I guess at the very
least, the documentation for each of these functions (pun intended) should
caution the programmer that the order of arguments may not be the same as
other similar functions.

-Mike

> From: Ariel Flesler
> 
> 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

Reply via email to