1. About function parameters - ES6 new feature 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 out the value of each argument.
For js, it allows to pass in any number of parameters, even if the number of actual parameters of the call is more than the number of formal parameters
2. Higher order functions
i.e. a function that can receive a function as its argument
Commonly used are map, reduce, filter, sort
map: I understand it to operate on each element in an array, such as:
var str = [1,2,3 ]; var strNew = str.map ( (x)=>{ return ++x; } ) alert(strNew)
reduce: receive two parameters and do cumulative operations, such as:
var str = [1,2,3 ]; var strNew = str.reduce ( (x,y)=>{ return x+y; } ) alert(strNew)
The above function can complete the accumulation of the array
filter: filter out the elements in the array that you don't want to keep, and decide whether to leave the element according to the return value of true or false, for example:
var str = [1,2,3 ]; var strNew = str.filter ( (x)=>{ return x%2==0; } ) alert(strNew)
The above code can achieve to keep the even numbers in the array
sort: sort array elements, such as:
var str = [71,9,0,12,106.23 ]; var strNew = str.sort ( (x,y)=>{ if (x>y) return 1; else if (x==y) return 0; else return -1; } ) alert(strNew)
In fact, it is sorted according to the custom comparison rules - the comparison between the previous element and the latter element returns -1