gpt4 book ai didi

javascript - 当我们在数组中有参数时如何调用函数

转载 作者:行者123 更新时间:2023-11-30 10:25:40 25 4
gpt4 key购买 nike

假设我们有一个函数

f = function(a, b, c){
// do something important
}

和一个包含参数的数组

var args = [5, 'string', 12] // just any values

显然,我可以这样调用我的函数:

f(args[0], args[1], args[2])

这真的很不优雅,我正在寻找一种更好的方法来完成它。感谢您的建议。

最佳答案

您正在寻找Function.apply()

f.apply(window, args); // pass something other than null to set 'this' within the call

关于javascript - 当我们在数组中有参数时如何调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19665948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com