Hello,
I am reading Javascript for Absolute Beginners and I have a question about the following referenced code…
function youSayGoodbye() {
alert("Goodbye!");
function andISayHello() {
alert("Hello!");
}
return andISayHello;
}
When I call the function youSayGoodbye() at https://jsfiddle.net/ I see one alert that says “Goodbye!”
Then when I initialize a variable:
var something = youSayGoodbye();
and call:
something()
I get two alerts 1st: Goodbye! and 2nd: Hello!
My questions are:
- The book says:
The youSayGoodbye outer function, from the something variable’s point of view, simply goes away. Because the something variable now points to a function, you can invoke this function by just calling it using the open and close parentheses like you normally would. When you do this, the returned inner function (aka andISayHello) will execute. Just like before, you will see a dialog box appear, but this dialog box will say Hello!—which is what the alert inside this function specified.
My issue is understanding why when I use something () at https://jsfiddle.net/ (One for Hello! and One for Goodbye!) When the wording from the book makes seem it like I am just supposed to get one (Hello!).
I am confused. My apologies for so much text… please explain (wipes sweat beads from head caused by noob programming anxiety. ) Thanks!