代码之家  ›  专栏  ›  技术社区  ›  user3552178

现在将rest参数传递给下一个func和args是不同的

  •  3
  • user3552178  · 技术社区  · 7 年前

    这是密码

    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);

    为什么第二个参数现在不同了,如何使它与第一个相同?

    1 回复  |  直到 7 年前
        1
  •  4
  •   CertainPerformance    7 年前

    (...args) => 在参数列表中,将参数转换为 阵列 命名的 args 是的。所以当你打电话 func1(args); ,你在打电话 func1 具有 参数,是一个数组的参数(而 func0 是和 参数)。

    如果你想打电话 功能1 使用三个原始参数 传播 改变 参数 数组到参数列表中:

    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);