JavaScript函数式编程
改写下列功能double,increment和foo
请不要将它们合并成一个功能!
通过应用DRY原则和功能程序模式与现代化的JavaScript特性。
使得它写的更地道的Javascript的方式。
function debounce(fn, delay) {
var timer = null;
return function () {
clearTimeout(timer)
timer = setTimeout(function () {
fn.call(this)
}, delay)
}
}
function a() {
console.log('hello')
}
var b = debounce(a, 1000)
for (let i = 0; i < 10; i++) {
b();
}
参考链接:
JavaScript函数式编程
const double = x => x * 2
const increment = x => x + 1
function foo(arrIn) {
return arrIn.map(x => increment(double(x)))
}
let arr = [1, 2, 3, 4]
console.log(foo(arr))