"callback" keyword in JavaScript -
please in understanding below code:
// define our function callback argument function some_function(arg1, arg2, callback) { // generates random number between // arg1 , arg2 var my_number = math.ceil(math.random() * (arg1 - arg2) + arg2); // we're done, we'll call callback , // pass our result callback(my_number); } // call function some_function(5, 15, function(num) { // anonymous function run when // callback called console.log("callback called! " + num); }); in above code,what callback keyword.what use of word. there no function defined name callback.
the gap in logic think you're having hard time anonymous, unnamed functions. once upon time, functions named. code written this:
function memberprocessingfunction() { // etc } function adminprocessingfunction() { // etc } var loginprocessingfunction; if (usertype == 'basicmember') { loginprocessingfunction = memberprocessingfunction; } else if (usertype == 'admin') { loginprocessingfunction = adminprocessingfunction; } loginprocessingfunction(); someone thought "this dumb. i'm creating function names use them in 1 place in code. let's merge together."
var loginprocessingfunction; if (usertype == 'basicmember') { loginprocessingfunction = function() { // etc }; } else if (usertype == 'admin') { loginprocessingfunction = function() { // etc }; } loginprocessingfunction(); this saves lot of time when you're passing function function argument. often, used "callbacks" - occasions want run code, after indeterminately-timed function has finished work.
for top function, callback name of third argument; expects function, , provided when method called. it's not language keyword - if did "find/replace all" of word "callback" "batmanvsuperman", still work.
Comments
Post a Comment