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.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn