Design mismatch between $ .map () & .map ()?

While the interface of the .map() method is .map( callback(index, domElement) ) , it is $.map( array, callback(elementOfArray, indexInArray) ) for $ .map () ... Any idea for a reason, why does $ .map () prefer to put the returned arguments in such an order as the value index?

+8
jquery
source share
3 answers

If you study the jQuery API, you will notice that all methods that work with a set of selected elements and accept callbacks, such as .each , .html , .text , etc., all pass the index to the element as the first argument, t .e. .map is here. Usually you access the current element with this inside a callback, this is just a generic template in jQuery, and so developers might decide that it is more important to have an index as the first argument.

On the other hand, the native Array.prototype.map method passes the value of the element as the first argument to the callback, so it seems to make $.map work the same way, as it is assumed that it uses a common set of elements.

+2
source share

Because the API is imperfect. It started inconsistently, but a fix that would now break existing code that uses $.map() .

+7
source share

Yes, there is a difference between jQuery (). map () and jQuery.map ().

To identify some differences:

jQuery.map () = this tends to iterate over a common set

jQuery (). map () = Objects like an array should be transferred to arrays (.makeArray ())

jQuery.map () = is mainly used to get or set a value a set of elements and return this new array

Also .map () is an ECMAScript5 function and is defined outside of jQuery (although implemented). I wonder if jQuery (). Map () was an older version .. map () was obviously faster!

John Resig states: "jQuery.map is primarily intended for use with arrays of DOM elements (as stated in the documentation). For this reason, it does things like strip out null / undefined and it smooths the arrays of nodes into a single array." This is defined in ECMA (Mozilla). Another implementation is Resig itself, hence the difference. This is not a bad design !!!!!! Read

Second reason: "It's not as simple as a" fix "... jQuery.map callback has a param parameter that corresponds to Array.prototype.map, while the jQuery.fn.map param parameter matches all other parameters of the iterator callback request jQuery. or another will tear backwards compatibility, which jQuery is working hard to avoid. " Read

+1
source share

All Articles