首页 > 解决方案 > 如何创建对象作为参数?

问题描述

我有以下片段:

function click(...actions: any) { 
  const [ a, b, c ] = actions;
  console.log(b);
}

let a = {
  show: [
    1, 2, 3, this.addService
  ]
};

click(a.show);

所以,我尝试a通过 key在对象中添加一些变量show

然后我想获取这些参数并传递给函数click()。

然后在函数中,click我试图获取 variables 中的所有参数[ a, b, c ]

我的问题在于:

show: [1, 2, 3, this.addService]

如何解决?

标签: javascripttypescript

解决方案


从函数参数中删除扩展运算符。

function click(actions: any) { 
  const [ a, b, c ] = actions;
  console.log(b);
}

let a = {
  show: [
    1, 2, 3, this.addService
  ]
};

click(a.show);

推荐阅读