Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.map vs $.fn.map different arguments

Tags:

jquery

I'm just wondering if anyone knows why $.map and $.fn.map pass arguments in flipped order from one-another. Is there a valid reason (e.g. ECMA specs somewhere) for it or was it just a poorly-planned API that is now impossible to fix due to the amount of code relying on jQuery?

$.map([ 'a', 'b', 'c' ], function(){ console.log(arguments); })
// ['a', 0], ['b', 1], ['c', 2]

$.fn.map.call([ 'a', 'b', 'c' ], function(){ console.log(arguments); })
// [0, 'a'], [1, 'b'], [2, 'c']

.each doesn't act like this

like image 685
Nobody Avatar asked Jul 11 '11 22:07

Nobody


People also ask

What is difference between each and map in jQuery?

The each method is meant to be an immutable iterator, where as the map method can be used as an iterator, but is really meant to manipulate the supplied array and return a new array. Another important thing to note is that the each function returns the original array while the map function returns a new array.

What is map() in jQuery?

This map() Method in jQuery is used to translate all items in an array or object to new array of items. Syntax: jQuery.map( array/object, callback )

How do you map an array of objects?

The syntax for the map() method is as follows: arr. map(function(element, index, array){ }, this); The callback function() is called on each array element, and the map() method always passes the current element , the index of the current element, and the whole array object to it.

How does map work in JavaScript?

The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally map() method is used to iterate over an array and calling function on every element of array.


1 Answers

Indeed, that might be considered as an oversight, but there is a very good reason for the arguments to be passed in that order.

Of course, on the surface, that's because the map() / each() methods and $.map() are not supposed to be equivalent, and each side is meant to be used in its own way (dealing with jQuery objects and arrays/hashes, respectively).

But the main point is that the parameter position is optimized for the case your callback function only wants to take a single argument. Consider both cases:

  • map() and each() are meant to be called on jQuery objects, and the callback function will be called in the context of each DOM element involved in the mapping/loop (the context object is available through the this keyword). So, it makes more sense to pass the index first, then the element, because this already designates the element.

  • $.map() deals with arrays and Javascript objects (hashes), and the callback function is invoked in the "global" context (this is always the window object). So, it makes more sense to pass the value first, then the index, because mapping is about avoiding loop index variables in the first place. If the caller is interested in those, he can use a "standard" for loop (or the second argument passed to the callback function).

like image 181
Frédéric Hamidi Avatar answered Oct 21 '22 12:10

Frédéric Hamidi