Can someone help me understand this code? Why is the anonymous function
inside brackets?
(function(food) {
if (food === "cookies") {
alert("More please");
} else if (food === "cake") {
alert("Yum yum");
}
})("cookies");
If I reference the function in a variable like this and remove the brackets I can understand the function is being invoked passing in the cookies
string.
var foodFunc = function(food) {
if (food === "cookies") {
alert("More please");
} else if (food === "cake") {
alert("Yum yum");
}
}
foodFunc("cookies");
Why would you use the first example? Why would you put the anonymous function in brackets?