Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何创建对象作为参数?_Javascript_Typescript - Fatal编程技术网

Javascript 如何创建对象作为参数?

Javascript 如何创建对象作为参数?,javascript,typescript,Javascript,Typescript,我有以下片段: function click(...actions: any) { const [ a, b, c ] = actions; console.log(b); } let a = { show: [ 1, 2, 3, this.addService ] }; click(a.show); 因此,我尝试通过键show在objecta中添加一些变量 然后我想得到这些参数并传递给函数click() 然后在函数中单击我试图获取变量[a,b,c]中的所有参数

我有以下片段:

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

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

click(a.show);
因此,我尝试通过键
show
在object
a
中添加一些变量

然后我想得到这些参数并传递给函数click()

然后在函数
中单击
我试图获取变量
[a,b,c]
中的所有参数

我的问题在于:

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

如何解决它?

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

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

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

click(a.show);

只需执行数组数据的销毁

函数单击([a,b,c]){
控制台日志(a、b、c);
}
设a={
展示:[
1,2,3,这是addService
]
};

点击(a.show)问题到底是什么?问题是我不知道如何在函数中传递对象作为参数,其中对象的属性是变量和函数,服务不能在参数列表中传播参数。是的,它可以工作,谢谢!您仍然可以将其设置为动态的,并使用任意数量的参数,使最后一个参数始终是函数,然后使用其余参数调用该函数。