是否可以使用 "call" 或 "apply" 将参数数组一次传递给多个函数?

Is it possible to use the "call" or "apply" to pass the an array of arguments to multiple functions at once?

例如,假设我有两个函数,abab 都有两个 string 参数,并且然后将这些字符串记录到控制台:

function a(w, x) {
  console.log(w, x);
}

function b(y, z) {
  console.log(y, z);
}

是否可以调用 a b 使用 callapply 将参数数组传递给同时执行这两个功能?

例如,这里是一些伪代码:

(a, b).apply(window, ["Wello", "Horld"]);

是否可以在不使用循环或不执行以下操作的情况下实现此目的:

var args = ["Wello", "Horld"];
a.apply(window, args);
b.apply(window, args);

另外,是否可以同时调用两个以上的函数?

注意: 函数可以 运行 以任何顺序排列。

谢谢。

您可能仍然需要对函数进行一些迭代,方法是将函数放入数组中并使用 Function#apply 调用它们。

function a(w, x) {
    console.log(w, x);
}

function b(y, z) {
    console.log(y, z);
}

[a, b].forEach(fn => fn.apply(window, ["Wello", "Horld"]));

Is it possible to call a and b using call or apply to pass an array of arguments to both functions at once?

...

Is it possible to achieve this without using loops or doing something this...

没有。您必须执行您所描述的操作,要么将参数放入数组中并进行每次调用,要么循环遍历函数数组等。

例如:

for (const f of [a, b]) {
    f.apply(window, ["Wello", "Horld"]);
    // Or if you have the arguments as discrete things like that, I'd use call:
    //f.call(window, "Wello", "Horld");
}

你需要使用某种循环。

const args = ["Wello", "Horld"];
[a, b].forEach(f => f.apply(Windows, args));

会成功的。这适用于您想要的尽可能多的功能。