Title said, in fact, refers to JS in the built-in function and apply the combination of use. This usage is very flattering, read the jQuery source of the small partners know, it is applied in the very wide and exquisite!
Pear: Find the maximum and minimum values in an array
The common practice is to iterate through the array, compare, write out the code looked like pomegranate sister, although a beautiful woman, but always feel a little awkward, how to turn her into like Chou-Heung as graceful and charming? Do you remember Math.max?
var numbers = [5, 6, 2, 3, 7]; /* */var max = Math.max.apply(null/*/var min = Math.min.apply (null, numbers);
What if the array is large? Cut it into a loop.
function Minofarray (arr) { var min = Infinity; var QUANTUM = 32768; for (var i = 0, len = arr.length; i < len; i + = QUANTUM) {var submin = Math.min.appl Y (null, Arr.slice (i, math.min (i + QUANTUM, Len))); = math.min (submin, Min); } return min;} var min = Minofarray ([5, 6, 2, 3, 7]);
Reference: MDN
Borrowing chickens to give birth to eggs in JavaScript