在 curry 函数中调用 null 的目的(JavaScript 实现)
Purpose of call with null in curry function (JavaScript implementation)
在 curry
函数 here 的以下实现中:
// curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
function curry(fn) {
const arity = fn.length;
return function $curry(...args) {
if (args.length < arity) {
return $curry.bind(null, ...args);
}
return fn.call(null, ...args);
};
}
在 fn.call
中将 call
与 null
用作 this
的目的是什么? fn(...args)
不行吗?我确实尝试了一些示例并得到了相同的结果。
是的,它会工作得很好。 (微小的区别在于,对于 this
上下文,它传递 undefined
而不是 null
)。
那么作者为什么要用.call(null, …)
呢?可能是因为它很好地反映了 .bind(null, …)
调用。
在 curry
函数 here 的以下实现中:
// curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
function curry(fn) {
const arity = fn.length;
return function $curry(...args) {
if (args.length < arity) {
return $curry.bind(null, ...args);
}
return fn.call(null, ...args);
};
}
在 fn.call
中将 call
与 null
用作 this
的目的是什么? fn(...args)
不行吗?我确实尝试了一些示例并得到了相同的结果。
是的,它会工作得很好。 (微小的区别在于,对于 this
上下文,它传递 undefined
而不是 null
)。
那么作者为什么要用.call(null, …)
呢?可能是因为它很好地反映了 .bind(null, …)
调用。