如何将参数的字符串列表传递给函数并将它们分配为单独的参数
How can I pass a string list of parameters to a function and have them assigned as separate arguments
如何在下面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调用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(',');