user5260143
user5260143

Reputation: 1098

javascript: pass / accept params to func

I have generic function at javasciprt. It accept : other func to call, and params to send. It looks like:

    function generic(func, funcArguments){
       //my Code...
       func(funcArguments);
    }

suppose I call generic func from func called myFunc:

   function myFunc(a, b){
        generic(foo, arguments);//argument will pass a and b, as js rules
   }


   function foo(a, b){
       ///my code
   }

The result is that foo function all of the argument as one entity that pass to parameter a.

it not good for me. I need it to accept a into a and b into b. But I cannot send them simply, becouse each time I accept differance count of params.

Is there any way to implement this logic?

Upvotes: 1

Views: 42

Answers (1)

Konstantin Dinev
Konstantin Dinev

Reputation: 34915

You would need to call the method will apply(context, arguments). Then the arguments will be provided as separate members of the arguments array, instead of just one argument.

function generic(func, funcArguments){
   func.apply(this, funcArguments);
}

Upvotes: 2

Related Questions