user3552178
user3552178

Reputation: 2971

passing rest args to next func and args is different now

This is the code

const func0 = (...args) => {
  console.error('-------0-------');
  console.error(args);
  console.error(args.length);
  func1(args);
}

const func1 = (...args) => {
  console.error('-------1-------');
  console.error(args);
  console.error(args.length);
}

func0(1, 2, 3);

why the 2nd arg is different now, how to make it the same as 1st one ?

Upvotes: 3

Views: 35

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370759

(...args) => in the parameter list transforms the arguments into an array named args. So when you call func1(args);, you're calling func1 with one argument, an argument that's an array (whereas func0 was called with three arguments).

If you wanted to call func1 with the three original arguments, use spread to transform the args array into a parameter list:

const func0 = (...args) =>  {
    console.error('-------0-------');
    console.error(args);
    console.error(args.length);
    func1(...args);
}

const func1 = (...args) => {
    console.error('-------1-------');
    console.error(args);
    console.error(args.length);
}

func0(1,2,3);

Upvotes: 4

Related Questions