1.高階函數:一個函數就能夠接收另外一個函數做爲參數,這種函數就稱之爲高階函數。函數
function power(x){ return x*x; } var arr = [1,3,5,9]; var result = arr.map(power); console.log(result);
[x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4) var arr = [1,3,5,7,9]; var result_sum = arr.reduce(function(x, y){ return x+y; }); console.log(result_sum); var result = arr.reduce(function(x, y){ return x*10+y; }); console.log(result);
var str = '123456'; function string2int(str){ var toInt = str.split('').map(function(ch){ return ch - '0'; }).reduce(function(x, y){ return x*10 + y; }); return toInt; } string2int(str);
var arr = [1, 2, 4, 5, 6, 9, 10, 15]; var r = arr.filter(function (x) { return x % 2 !== 0; }); r; // [1, 5, 9, 15]