gpt4 book ai didi

javascript - 如何将参数的字符串列表传递给函数并将它们分配为单独的参数

转载 作者:行者123 更新时间:2023-12-02 15:40:01 24 4
gpt4 key购买 nike

如何在下面的 Javascript 中调用 myFunc

var myParams = "'a','b','c','d'";
myFunc(myParams); // myFunc("'a','b','c','d'");

而我希望它的行为如下:

myFunc('a','b','c','d');

最佳答案

使用apply()

var x = "'a','b','c','d'";
var args = x.split(',');
myfunc.apply(null, args);

这相当于

myfunc('a', 'b', 'c', 'd');

更新

When I call directly the alert just shows the value without enclosing any quotes. But by your method it shows the value enclosed with single quotes like 'a'

var args = x.replace(/[']+/g, '').split(',');

关于javascript - 如何将参数的字符串列表传递给函数并将它们分配为单独的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32662188/

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