-1

I have a function pool:

var func0 = function(a0){...}
var func1 = function(a0, a1){..}
var func2 = function(a0, a1, a3){...}

Now I got the function name and a arguments array:

var name = 'func1';
var params = [arg0, arg1];

Name is a dynamic string so I can't write like:

func1(arg0, arg1);

Is there any way can let the params passed into the function like reflection in java?

Note without using "arguments":

func(){ console.log(arguments[0]);}

@Felix Kling this is not a duplication, my question is how to pass variable parameters, and i know the way obj[name] but this doesn't help my question.

My question is how to convert params[arg0, arg1] into func(arg0, arg1), not func(params) and not func(){arguments[0]...argument[1]...}

4
  • Related: Javascript dynamically invoke object method from string Commented Sep 20, 2016 at 14:08
  • this way:function func1(a){alert(a)};window["func1"](2) Commented Sep 20, 2016 at 14:12
  • @FelixKling this question is not a duplication. I know obj[name] but this doesn't help my question Commented Sep 20, 2016 at 15:05
  • It's not a duplicate of the question in the comments, but a duplicate of the question it is closed as duplicate of. Did you actually look at it? Commented Sep 20, 2016 at 15:06

1 Answer 1

0

Use the eval function, like this

eval(name+"("+params[0]+","+params[1]+")");
Sign up to request clarification or add additional context in comments.

1 Comment

ah...maybe eval is not a good way...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.