javascript - Use a function with parameters inside a callback -
i've written function may take unknown number of functions parameters, can't figure how can make work when 1 or more of functions take parameters too.
here quick example of achieve:
function talk(name) { console.log('my name ' + name); var callbacks = [].slice.call(arguments, 1); callbacks.foreach(function(callback) { callback(); }); } function hello() { console.log('hello guys'); } function weather(meteo) { console.log('the weather ' + meteo); } function goodbye() { console.log('goodbye'); } // able following: //talk('john', hello, weather('sunny'), goodbye);
you can pass anonymous function can call function required parameters
talk('john', hello, function(){ weather('sunny') }, goodbye); function talk(name) { console.log('my name ' + name); var callbacks = [].slice.call(arguments, 1); callbacks.foreach(function(callback) { callback(); }); } function hello() { console.log('hello guys'); } function weather(meteo) { console.log('the weather ' + meteo); } function goodbye() { console.log('goodbye'); } talk('john', hello, function() { weather('sunny') }, goodbye);
Comments
Post a Comment