如何动态地将参数从高阶函数传递给回调函数?

时间:2019-06-28 03:52:18

标签: javascript dynamic parameters callback higher-order-functions

我有使用n参数的回调函数。 n是动态的。如何将n参数从高阶函数动态传递给回调函数?

const callbackFn1 = (para1, para2, para3) =>{
 //some code
}
const callbackFn2 = (para1, para2, para3, para4) =>{
 //some code
}
const hoFn = (callback, ...rest){
 return callback(rest[0], rest[1], rest[3],...,rest[n-1]) // how to pass like this?
}

hoFn(callbackFn1, para1, para2, para3)
hoFn(callbackFn2, para1, para2, para3, para4) 

1 个答案:

答案 0 :(得分:1)

function hoFn(callback, ...rest) {
  callback(rest);
}
hoFn((params) => {
  console.log(params);
}, "Lorem", "Ipsum", "lora")