【发布时间】:2019-04-03 16:54:32
【问题描述】:
我正在尝试让中间件函数调用它调试,它应该接受一些参数,记录它们并传递给下一个函数:
const debug = (...args) => {
console.log(...args)
return args // will return array, not argument type variable
}
const compose = (...fns) => (...arg) => (
fns.slice().reverse().reduce(
(acc, fn) => !acc ? fn(...arg) : fn(acc), null
)
)
const f = x => x * x
const g = (a, b) => a + b
const makeMagic = compose(
f,
g,
debug
)
makeMagic(1, 2)
如果我从组合中删除调试,一切都会按预期工作,只要我把它放在最后,它就会中断。因为它接受参数但返回数组。
我试图用这种方式重写debug:
function debug() {
console.info(arguments)
return arguments
}
但它绝不会失败。
【问题讨论】:
-
当
debug被多个参数调用时,return args应该返回什么? -
也许这个问题有助于获取数组:stackoverflow.com/q/54568053/1447675
-
@Mark Meyer 它应该返回相同数量的参数。
标签: javascript