I'm reading a chapter of a book dealing with closures and scoping.
Let's start from two examples (I copy them from the book). Consider the following code:
var scope = "global space";
function checkscope(){
var scope ="local scope";
function f() { return scope; }
return f();
}
checkscope();
here the value that we obtain calling checkscope() function is "local scope".
Now, consider this function:
var scope = "global space";
function checkscope(){
var scope ="local scope";
function f() { return scope; }
return f;
}
checkscope()();
It seems similar but it isn't, we have return f rather than return f() inside checkscope function and checkscope itself is called at the end with two pairs of parentheses rather than one couple.
Could you help me to understand these two variations? I don't understand why f function is called without parentheses: it's a function, if you want to call it you need to use the parentheses, I guess. Then, about checkscope function, why all those parentheses?