Passing arguments to another javascript function

If you which to forward a call from one function to another passing all the parameters which were sent to the first function this can be done by using apply and passing arguments:
function myFunction(args){
myOtherFunction.apply(this, arguments);
}

function myOtherFunction(fieldA, fieldB){
alert(fieldA + " " + fieldB);
}

myFunction('hello','world');

Please note!The call to apply uses arguments and not args. This is intended and not a typo. The parameter args allows the function myFunction to receive arguments. The arguments object is an array of all the parameters sent to the function. If you don't want to specify which parameters myOtherFunction takes you can use arguments[0] etc in order to access the posted parameters.

Related posts:

Comments

comments powered by Disqus