0

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!!

8
  • You need eval() in your case, but why you store functions as strings in array? Commented Jan 22, 2014 at 21:05
  • @Givi: I don't think u can store function as function in array in JS... Commented Jan 22, 2014 at 21:06
  • 2
    You can! 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. Commented Jan 22, 2014 at 21:07
  • jsFiddle Commented Jan 22, 2014 at 21:11
  • ok, i'm sorry... my eyes played trick on me... I actually thought of it before and for some reason i insist that it doesn't work... Commented Jan 22, 2014 at 21:12

1 Answer 1

2

If you encapsulate the function in a module (object), you could call it like so:

JavaScript

var module = {
    argumentAsString: function(){..}
}

var arr = [
    "argumentAsString",
    123,
    "function(){console.log('this is a function');}"
];

function chal(){
    for(var i = 0, iLen = arguments.length; i<iLen;i++)
        if(module.hasOwnProperty[arguments[i]])
            //if argument is a function, call it
            module[arguments[i]]();
        else
            //if argument is NOT a function, console.log the value
            console.log(arguments[i]);
}

EDIT

I may have misinterpreted the question! :p

Try assigning that function to a variable, and storing that variable in the array instead:

var fcn= function(){
    console.log('this is a function');
}

var arr = [
    "argumentAsString",
    123,
    fcn
];

EDIT 2

If I'm right, neither of the above two answers will solve your problem. See this similar question: Convert string (was a function) back to function in Javascript

Sign up to request clarification or add additional context in comments.

1 Comment

You cannot use function as a variable identifier. Otherwise, yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.