如何在下面的 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/