I have a function (named “chal") which simply console.log() the arguments if they are NOT a function or call the function otherwise.
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]);
}
I have an array (named “arr") which contain a function “AS A STRING”
var arr = [
"argumentAsString",
123,
"function(){console.log('this is a function');}"
];
I need “chal" function to run with arguments from “arr” array but the function as string in the array get called as a function.
I know it's very confusing... here is jsFiddle: http://jsfiddle.net/YqBLm/1/
I know it's wield but I actually ran into a situation when I need to do such thing... I basically passed the function from server-side to client as string (function.toString()) and now i need to pass it as an argument to a function on client-side... Can anyone think of anything?
Thanks a lot in advance!!
var arr = ["argumentAsString", 123, function(){console.log('this is a function');}]; arr[2]();since, functions is just object you can store reference of that function in variables or in arrays or in another objects.