Tag: 高阶函数

在javascript中使用更高阶函数的原型函数

我试图concat使用减less数组的数组,我想我可以使用Array.prototype.concat函数是这样的: arr = [[1],[2],[3]] arr.reduce((a, b) => Array.prototype.concat(a, b), []) 哪些工作正常,并给我的数组[1, 2, 3] 。 然后我觉得我可以变得更聪明,像这样做: arr = [[1],[2],[3]] arr.reduce(Array.prototype.concat, []) 但是,这给我一个错误: TypeError: Array.prototype.concat called on null or undefined at Array.reduce (native) at Object.<anonymous> (/home/axel/Developer/temp/reduce2.js:2:5) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.runMain (module.js:590:10) at run (bootstrap_node.js:394:7) […]

JavaScript减less不能处理math函数?

我正在尝试一个明显的任务: var maxVal = [ 1, 2, 3, 4, 5 ].reduce( Math.max, 0 ); 并得到: NaN 作为结果。 为了使其工作,我必须这样做一个匿名函数: var maxVal = [ 1, 2, 3, 4, 5 ].reduce( function ( a, b ) { return Math.max(a, b); }, 0 ); 有人能告诉我为什么吗? 这两个函数都带有两个参数,都返回一个值。 有什么不同? 另一个例子可能是这样的: var newList = [[1, 2, 3], [4, 5, 6]].reduce( Array.concat, [] […]