Help me understand these following piece of codes. I'm still newbie in JavaScript.
We know that the following code will run alertbox with A message
// 1
function box() {
alert('A');
return function() {
alert('B');
}
}
var x = box();
And we know that the following will run alertbox A then followed by alertbox B
// 2
function box() {
alert('A');
return function() {
alert('B');
}
}
var x = box();
x();
BUT in code below the behavior is just like snippet no 1. I expect it to run ONLY alertbox B.
// 3
function box() {
alert('A');
return function() {
alert('B');
}
}
var x = box; // no invoking box function
x();
QUESTION: Why did this happen to snippet 3? Didn't we only call x function? From snippet 1 and 2 we know that running x() will trigger alertbox B, but why didn't it appear on snippet 3?