我有一个函数(名为“CHAL“),这只是CONSOLE.LOG()的参数,如果他们没有一个函数或其他方式调用该函数。函数参数作为字符串,但作为函数执行?
function chal(){
for(var i = 0, iLen = arguments.length; i<iLen;i++)
if(typeof arguments[i] == "function")
//if argument is a function, call it
arguments[i]();
else
//if argument is NOT a function, console.log the value
console.log(arguments[i]);
}
我有一个数组(名为“改编”)它包含一个函数“作为字符串”
var arr = [
"argumentAsString",
123,
"function(){console.log('this is a function');}"
];
我需要“CHAL”功能与‘ARR’数组,但函数的参数运行的阵列中的字符串被调用的函数。
我知道它很混乱......这里是jsFi ddle:http://jsfiddle.net/YqBLm/1/
我知道它可以运用,但当我需要做这样的事情时我遇到了一种情况......我基本上通过函数从服务器端向客户端传递字符串(function.toString()),现在我需要将它作为参数传递给客户端的函数...任何人都可以想到任何东西?
非常感谢!
你需要[eval()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval),但为什么你在数组中存储函数作为字符串? – Givi
@Givi:我不认为你可以将函数作为函数存储在JS中的数组中...... –
你可以! var arr = [“argumentAsString”,123,function(){console.log('this is a function');}]; arr [2]();'因为函数只是对象,您可以将该函数的引用存储在变量或数组中或另一个对象中。 – Givi