4

I"m new to Javascript and programming in general and came upon this block of code from a book called Javascript Enlightenment (p.88):

var parentFunction = function() {
    var foo = 'foo';
    return function() { // anonymous function being returned
        console.log(foo); // logs 'foo'
    }
}
// nestedFunction refers to the nested function returned from parentFunction

var nestedFunction = parentFunction();

nestedFunction(); /* logs foo because the returned function accesses foo
via the scope chain */

Why does setting var nestedFunction = parentFunction(); enable nestedFunction(); to invoke the nested anonymous function and log "foo" to the console whereas using just parentFunction(); logs nothing at all?

1

6 Answers 6

5

Invoking parentFunction returns the anonymous function without calling it.

nestedFunction gets set as the return of parentFunction, the anonymous function.

Invoking nestedFunction hence invokes the anonymous function.

The anonymous function uses console.log so you see "foo".

Sign up to request clarification or add additional context in comments.

Comments

3

Basically you're doing:

parentFunction()(); // double parenthesis

Parenthesis mean that you execute the function, which will return a value. If that value is a function you can then execute it.

If you call it only once, well, you just get the function so nothing gets console.logged

1 Comment

Aha! This makes sense. Thank you for your clear and concise explanation and thanks also to everyone else who answered.
1

An alternative to your code is this

var parentFunction = function() {
  var foo = "foo";
  return console.log.bind(console);
}

parentFunction()();
// => "foo"

Inevitably, you'll want to do something with scope at some point, so you'd do it like this

var parentFunction = function() {
  this.foo = "foo";
  this.something = function(){
    console.log(this.foo);
  }
  return this.something.bind(this);
}

parentFunction()();
// => "foo"

Comments

1
function add (x) {
    return function (y) {
        return x + y;
    };
}
var add5 = add(5);
 add5(3);

Explained:

When the add function is called, it returns a function. That function closes the context and remembers what the parameter x was at exactly that time (i.e. 5 in the code above) When the result of calling the add function is assigned to the variable add5, it will always know what x was when it was initially created. The add5 variable above refers to a function which will always add the value 5 to what is being sent in. That means when add5 is called with a value of 3, it will add 5 together with 3, and return

Please refer this link...

http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/

Comments

0

Because parentFunction is returning the nested function, which needs to be invoked in order to run.

var a = nestedFunction;

doesn't log anything, because it hasn't been executed, until you do this:

a();

Comments

0

Rewrite that code as this:

var nestedFunction = null;

var parentFunction = function() {
    var foo = 'foo';
    nestedFunction = function() { // anonymous function being returned
        console.log(foo); // logs 'foo'
    }
}

parentFunction(); 
// outputs nothing
// but initializes nestedFunction by its local function

nestedFunction(); /* logs foo because that local function has access to local 'foo' of
the parent function */

As you see parent function outputs nothing but initializes nestedFunction variable by function reference. And that function reference can be called as any other function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.