A problem of function parameter .arg

function __matchArgs__(fn) {
    return function (...args) {
        console.log(args)
        if (args.length !== fn.length) {
            throw RangeError("Arguments not match!");
        }
        return fn.apply(this, args);
    }
}

var add = __matchArgs__((a, b, c) => a + b + c);

console.log(add(1, 2, 3));

I would like to ask return function (. ARGs) shouldn"t the arg here be the formal argument of the function _ _ matchArgs__? Why does log come out as a formal parameter in fn?

Mar.31,2021

matchArgs the formal parameter fn, function returns an anonymous function with its own indefinite parameters. The formal parameters of the function are determined at the time of definition, and are independent of each other ~


.

it is obvious when it is written like this:

function __matchArgs__(fn) {

  function returnFn(...args) {
    console.log(args)
    if (args.length !== fn.length) {
      throw RangeError('Arguments not match!');
    }
    return fn.apply(this, args);
  }
  return returnFn
}

you just "incidentally" defined this function in return, which is essentially a separate function. For the parameter part of a function, the parameter part does not have any connection with the external scope without using the default parameter

Menu