javascript - Anonymous Callback Function Clarification -
i'm brushing on callback functions , came across following passage http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/#
"when pass callback function argument function, passing function definition. not executing function in parameter. in other words, aren’t passing function trailing pair of executing parenthesis () when executing function.
and since containing function has callback function in parameter function definition, can execute callback anytime."
can explain that? here 2 examples provided.
//the item callback function $("#btn_1").click(function() { alert("btn 1 clicked"); });
here example:
var friends = ["mike", "stacy", "andy", "rick"]; friends.foreach(function (eachname, index){ console.log(index + 1 + ". " + eachname); // 1. mike, 2. stacy, 3. andy, 4. rick });
"note callback function not executed immediately. “called back” (hence name) @ specified point inside containing function’s body. so, though first jquery example looked this:
//the anonymous function not being executed there in parameter. //the item callback function $("#btn_1").click(function() { alert("btn 1 clicked"); });
the anonymous function called later inside function body. without name, can still accessed later via arguments object containing function."
for first example jquery, saying exactly. if #btn_1 element clicked, anonymous function executed? assuming executed if button clicked, wording passage confusing?
similarly, second example, not need call function passed argument bc anonymous?
in both examples, passing anonymous function parameter.
$("#btn_1").click(function() { alert("btn 1 clicked"); });
jquery's click method takes function first parameter. imagine click's function definition this:
function click(fn) { // fn contain reference // function passed first parameter click // merely calling fn nothing, because 'calling' // reference. fn; // since inside of fn function, can execute // () syntax fn(); } // now, have many ways pass function first parameter function // 1. anonymous function: click(function() { console.log("hi"); }); // 2. named function: click(function hello() { console.log("hi"); }); // 3. reference function declaration function hithere() { console.log("hi"); } click(hithere); // 4. variable holds anonymous function inside var howdy = function () { console.log("howdy"); }; click(howdy);
just imagine functions variables, have content inside can executed ()
@ end.
function hi() { console.log('bye'); } hi; // calls reference, not execute it. nothing. hi.tostring(); // returns function string hi(); // executes code within function
whenever declare named function, can stuff according name, variables. of course, unlike variables, hold executable code inside, , not values.
you can't reference anonymous function, because it's well... anonymous. unless, hold inside of has name, var
.
var iholdafunctioninside = function () { console.log('im not anonymous now'); }; iholdafunctioninside(); // logs "im not anonymous now"
and why can pass anonymous function parameter function, , can execute callback. because parameter 'holds' anonymous function inside of it:
function iexecuteyourcallback(callback) { // callback contains anonymous function passed // similar doing: // var callback = function () { }; callback(); } iexecuteyourcallback(function() { console.log('im callback function!'); });
hope helps clear things bit.
Comments
Post a Comment