In short, I would stick with passing variables to avoid scope conflicts and keep the simultaneous calls working correctly. However, I would change the format in which you pass them:
Because JavaScript is so flexible with objects, I like to have one all-encompassing parameter for functions that may require several parameters. This improves visibility when calling a function, simplifies the extension of the function to support additional parameters and additional parameters, and in your case, simplifies the transfer of parameters to additional functions in the chain. The only drawback is the longer function calls, but I find it worth it. For instance:
/** * .. * * params.test1 Test 1 does blah * params.test2 Test 2 does blah 2 * params.test3 Test 3 does blah 3 * params.test4 Test 4 does blah 4 */ function test(params){ //Initialize Parameters var test1 = params.test1; var test2 = params.test2; //function code .. test2(params); .. } /** * .. * * params.test1 Test 3 does blah 3 * params.test2 Test 4 does blah 4 */ function test2(params){ var test3 = params.test3; var test4 = params.test4; //function code using test3 and test4 .. } //Call the test function test({test1: 'foo1', test2: 'foo2', test3: 'foo3', test4: 'foo4'});
source share