提问人:Micheal Morono 提问时间:2/18/2022 更新时间:2/18/2022 访问量:45
使用一个函数执行另一个函数(具有可变数量的参数) 打字稿
use a function to execute another function (with a variable amount of parameters) Typescript
问:
我正在尝试创建一个函数,该函数可以使用可变数量的参数重复另一个函数。但是,我只得到了对匿名箭头函数的引用,而不是我尝试传入的函数(func)的返回值。我试过func()。但似乎
const testFunction1 = (x:number,y:number) => {
return {x,y}
}
const testFunction2 = (foo:string, bar:string, baz:string) => {
return {foo, bar, baz}
}
const repeatFunction = (num: number, func: () => unknown) => {
const returnValues: Array<(unknown)> = []
for (let i = 0; i < num; i++) {
returnValues.push(func())
}
return returnValues
}
const result = repeatFunction(5, () => testFunction1(4,5))
console.log(result)
真正奇怪的是,当我查看操场时,这似乎工作正常。但是当我使用编辑器和调试器时,出现以下错误
错误未捕获的 TypeError:func 不是函数
答:
0赞
Symphony
2/18/2022
#1
很可能您没有通过此功能屏幕() => testFunction1(4,5)
评论
func