dimanche 17 juillet 2016

Why does one call $.makeArray() for passing array-like objects to $.map()?

The following is written in the jQuery docs for $.map:

Array-like objects — those with a .length property and a value on the .length - 1 index — must be converted to actual arrays before being passed to $.map().

Is there a reason for this? If you try it with an array-like object, you can see that the result of $.map() does not change if you call $.makeArray():

var fruits = {"length": 3, 0: "Banana", 1: "Apricot", 2: "Peach"};
$.map(fruits, function(val, i) { return val; }); // Array [ "Banana", "Apricot", "Peach" ]
$.map($.makeArray(fruits), function(val, i) { return val; }); // Array [ "Banana", "Apricot", "Peach" ]

Aucun commentaire:

Enregistrer un commentaire