Why are jQuery's callback arguments inconsistent? Why are jQuery's callback arguments inconsistent? jquery jquery

Why are jQuery's callback arguments inconsistent?


It is not totally random. Because :

$.map( $('selector'), function(el, index) { /* element (DOMElement) is first, index optional */ } );$('selector').map(function(index) { /* 'this' (DOMElement) is first.... index optional */ });

See the pattern? The second example has a second argument, but it is only passed by convenience, and it is the same as this.

The pattern is that the first argument is always "more" important than the second, and the last argument should be the least important (the "more optional"). So you don't need to specify all the least important arguments if you only need one. And in the case of $(...).each, often you won't even need any argument, because this is only what you want.


That's frustrated me also at times -- $.each is the one that I always mess up.

I think it's due to different people/teams working on different parts of the framework. It's a community-driven framework, so nobody probably caught it early on and now that the framework is so wide-spread, they can't fix it without breaking 35% of all the sites on the Internet.

I don't think it will be fixed -- at least that's my opinion/attitude. I'm just going to have to commit them to memory and hope for the best!


Since Javascript lets you ignore parameters that you aren't using (i.e., you can define a callback function which takes only one parameter, even if it will be called with two), generally, the first parameter is the one you are mostly likely to be using. (Actually, the this varaible is usually the data item you'r most likely to use, followed by the first parameter, etc)