-2

Can someone please explain the code below? I'm confused about the role of return f();

  1. Does return f(); call the method f and return the result? (i.e. the return of f)

  2. Does return f(); return an object?

If I change return f(); to just f(); then checkscope = undefined

var scope = "global scope";          // A global variable
function checkscope() {
    var scope = "local scope";       // A local variable
    function f() { return scope; }   // Return the value in scope here
    return f();
}
checkscope()                         // => "local scope"
VLAZ
  • 26,331
  • 9
  • 49
  • 67
stckpete
  • 571
  • 1
  • 4
  • 13
  • 1
    1) yes 2) no, it returns a string 3) if you remove the `return` then `checkscope` doesn't return anything, hence you will get `undefined`. – VLAZ Dec 01 '16 at 17:24
  • Possible duplicate of [How do JavaScript closures work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – VLAZ Dec 01 '16 at 17:24
  • 1
    F returns exactly what it says it does. The point here is to show ES5 scoping, e.g., there's only function scope. – Dave Newton Dec 01 '16 at 17:24
  • 1
    There's no closure here. The function `f` isn't returned, so the environment isn't stored after the `checkscope` function finishes. – Quentin Dec 01 '16 at 17:26
  • 1
    @Quentin: Yes, there is. `f` is a closure, even though it doesn't survive the call. That's why it has access to `scope` within `checkscope`. – T.J. Crowder Dec 01 '16 at 17:28

2 Answers2

3
  1. Yes. return returns whatever the expression on the RHS resolves to
  2. You can see what f returns: return scope;, and scope is assigned on the previous line. It is a string.

If I change return f(); to just f(); checkscope = undefined

Well yes. If you don't have a return statement in a function, it returns undefined

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • ok thanks , Just to clarify - return f() calls the method f(); the method f returns to value of var scope (to the caller). Finally the string 'global scope' is returned – stckpete Dec 01 '16 at 17:33
  • @stckpete: Yup. But it's a function, not a method. – T.J. Crowder Dec 01 '16 at 17:38
1

It's #1. return f() calls f and then returns what f returns. The fact that f is a closure over the call to checkscope means that it has access to scope within that call's context, and so f reads the current value of scope, which is "local scope" (a string), and returns that value.

Does return f(); return an object?

No, a string (see above).

If I change return f(); to just f(); then checkscope = undefined

That's because when you call a function that doesn't terminate with a return <value> statement, the result of calling it is the value undefined. By removing the return, you remove the return value of checkscope.


Here's a more interesting version:

function memo(value) {
    function f() { return "value is '" + value + "'"; }
    return f; // Note! No (), not calling it, returning the function
}
var f1 = memo("hi");
var f2 = memo("there");
f1(); // "value is 'hi'"
f2(); // "value is 'there'"

That takes it a step further, allowing f (the closure) to exist beyond the end of the call to memo, which keeps the context of that specific call to memo alive, which means we can use the value specific to that context.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875