javascript - How to pass Array of Js functions to another Js function? -
i newbie java script. writing asynchronous functions let's func1, func2, func3. have execute 1 after another. decided use async lib execute 1 after another.
script file:
function func1(param1,param2){ //dosomething .. } function func2(param3,param4){ //dosomething .. } function func3(param5,param6){ //dosomething .. } function myfunction(arr){ console.log(arr); async.series(arr,function(){ //do .. }); } html file:
<a onclick="myfunction([func1('p1','p2'),func2('p3','p4'),func('p5','p6')])"></a> but when try console.log, giving null,null,null
kindly provide me solution regarding.
you're not passing array of function references, you're passing array of result of calling functions. array contain whatever functions returned (or undefined if don't return anything).
if want create function reference arguments "baked in", can function#bind:
myfunction([func1.bind(null, 'p1','p2'), ...]) // ^^^^^^^^^^^^ ^ function#bind creates new function that, when called, call original given this value (i've used null above on assumption this isn't important) , arguments give bind (followed arguments new function called with).
example:
myfunction([ func1.bind(null, 'p1', 'p2'), func1.bind(null, 'p3', 'p4'), func1.bind(null, 'p5', 'p6') ]); function func1(arg1, arg2) { console.log("func1 got (" + arg1 + "," + arg2 + ")"); } function myfunction(functions) { functions.foreach(function(f) { f(); }); }
Comments
Post a Comment