柯里化函数内部实现问题

function sub_curry(fn) {
    console.log(arguments)
    console.log(fn)
    let args = Array.prototype.slice.call(arguments, 1)
    return function () {
        let newArgs = args.concat(Array.prototype.slice.call(arguments))
        return fn.apply(this, newArgs)
    }
}

function curry(fn, length) {
    // 获取需要柯里化函数的参数长度
    length = length || fn.length;
    var slice = Array.prototype.slice;
    return function () {
        if (arguments.length < length) {
            let combined = [fn].concat(slice.call(arguments));
            console.log("combined:", combined)
            return curry(sub_curry.apply(this, combined), length - arguments.length);
        } else {
            return fn.apply(this, arguments)
        }
    }
}

在sub_curry中
console.log(arguments)和console.log(fn)为什么不同?
fn为何为一个函数不应该是下方的combined数组吗?

阅读 1.3k
1 个回答
  1. argument[0] === fn
  2. apply 的第二个参数为数组,数组成员会被“摊开”后作为被 apply 的函数参数列表:
fn.apply(null, [a, b, c]);

基本等同于:

fn(a, b, c);

说它们“基本等同”是因为 this 指向不一样。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题