我正在try 调用一个函数所需的次数.为了解决这个问题,我创建了一个接受rest个参数并迭代第一个参数的函数.我无法解包REST参数并将其传递给函数.有没有一种方法可以解包并将其作为参数传递给函数.以下是我的工作代码.有没有更好的办法让它发挥作用?先谢谢你.感谢任何人的帮助.
function hello(name) {
console.log("Hello "+ name);
}
function greet(name,time_of) {
console.log("Good " + time_of +" " +name);
}
function foo(times,x, ...args) {
for(i=0;i<times;i++) {
x(arguments)
//x(args); //works good for hello() but not for greet(). Doesn't pass them to second argument
x(args[0],args[1]); //This works but not scalable
//args.map((element) => x(element));
}
}
foo(2,hello,"Myname");
foo(3,greet,"Myname","Afternoon");