字节:一道 JS 基础编程题(闭包)
题目:
var foo = function(...args) {
// 要求实现函数体
}
var f1 = foo(1,2,3); f1.getValue();
// 6 输出是参数的和
var f2 = foo(1)(2,3); f2.getValue();
// 6
var f3 = foo(1)(2)(3)(4); f3.getValue();
// 10
解答:(@Ishmael-Yoko)
function foo(...args) {
const target = (...arg1s) => foo(...[...args, ...arg1s])
target.getValue = () => args.reduce((p, n) => p+ n, 0)
return target
}
来自:https://github.com/Advanced-Frontend/Daily-Interview-Question
最后
评论