Closed SOSANA closed 9 years ago
Hi Zach, You're writing a function expression and then calling the function. Calling:
eat("cookies")
(eat)("cookies")
(eat("cookies"))
each does exactly the same thing. The parentheses around the function name and the entire function call essentially do nothing. In the example on page 509/513 you're referring too, we were showing that you could get rid of the variable name eat altogether. However, in that scenario, you do need the parentheses (around the function expression) in order to make it work correctly.
I'm curious why you are saying this is a "callback"? Generally we say a function is a callback if it's being used as an event handler, or passed to another function to call later on. In this example, we'd just say it's a function.
Thank you for pointing that out. It is not a callback. It would be correct to say calling or invoking the eat function with an argument of "cookies".
// Here's the function extracted. We just called it eat. You could have made this a function declaration
// after anonymous function
var eat = (function(food){
if (food === "cookies") {
alert("More please");
} else if (food === "cake") {
alert("Yum yum")
}
});
// calling eat function with argument of "cookies"
(eat)("cookies");
// or you can do a call this way
//(eat("cookies"));
Hi,
Curious if there is a difference between the way we do callbacks shown below
What's the difference? advantage or disadvantage? or do they same thing just different syntax choice?