Let’s look at a piece of code first:
var a = [ 1,2,3];
function map(fn, a, init){
var s = init;
for (i = 0; i < a.length; i ){
s = fn(s,a[i]);
}
return s;
}
alert(map(function(x,y){return x y;}, a, 0)) //Add and sum the elements of the array
alert(map(function(x,y){return x y;}, a , "")) //Connect the elements of the array
The map function defines a traversal of the a array once, but the specific operation to be performed on each element is not defined and needs to be defined from its first parameter.
This approach can improve code reusability. good.