1, about function parameter--es6 new characteristic rest
Rest saves the parameters of the function in an array-like way
function Eledis (... rest) { for (var i=0;i<rest.length;i++ ) { alert (rest[i]); } } Eledis (1,2,[3,4,5]);
The Eledis function prints the value of each argument.
For JS, it is allowed to pass in any parameter, even if the number of arguments that are called is more than the formal parameter
2. High-order function
A function that can receive a function as its argument
Commonly used are map, reduce, filter, sort
Map: I understand that it is the operation of each element in an array, such as:
var str = [N/a ]; var strnew = Str.map ( (x)+= {return + +x; } ) Alert (strnew)
Reduce: Receive two parameters, do cumulative operations, such as:
var str = [N/a ]; var strnew = str.reduce ( (x, y)={ return x+y; } ) alert (strnew)
The above function can complete the accumulation of the array
Filter: Filters out elements in the array that you don't want to keep, depending on whether the return value is True or false determines the element's fate, such as:
var str = [N/a ]; var strnew = Str.filter ( (x)+= {return x%2==0; } ) Alert (strnew)
The above code can implement preserving even numbers in the array
Sort: Sorts an array of elements, such as:
var str = [71,9,0,12,106.23]; var strnew = Str.sort ( (x, y)= ={ ifreturn 1; Else if return 0; Else return -1; } ) Alert (strnew)
It's actually sorted according to the custom comparison rule ——— the previous element and the subsequent element comparison return to 1
"Front-end" JavaScript functions