I'm trying to learn about closures and came across this bit of code from MDN. I tried it in jsbin and it works, but I can't figure out why it's necessary to assign makeFunc to var myFunc and then calling myFunc rather than just calling makeFunc, which doesn't work.
function makeFunc() {
var name = "Mozilla";
function displayName() {
alert(name);
}
return displayName;
}
var myFunc = makeFunc();
myFunc();
makeFunc()();works. Or if you change thereturntoreturn displayName();,makeFunc();would then work.var myFunc = makeFunc();. Not sure I understand what you are confused about.makeFunc()does not trigger the alert. Or at least that's my guess.