0

I had a little understanding of closure which, a function that can be stored as a variable (referred to as a "first-class function"), that has a special ability to access other variables local to the scope it was created in.

Somehow I had trouble how this code works, how does these independent functions messageForRahul/Greg gets executed inside the Inner function when they are not declared as a parameter?

var sendMessageTo = function (name) {

return function (message) {
    console.log ("Message for " + name + ": " + message);
}
};

var messageForRahul = sendMessageTo ("Rahul");
var messageForGreg = sendMessageTo ("Greg");


messageForRahul ("Hello, Rahul");
messageForGreg ("Hello, Greg");
  • 1
    Have you read the [MDN article on closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) – adeneo Jul 17 '16 at 15:56
  • Why did you expect the code not to work? – Bergi Jul 17 '16 at 18:02
  • 1
    Beautiful article on closures: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work – Cheng Sieu Ly Jul 17 '16 at 18:05
  • @Bergi The code works as it should – P.S. Jul 17 '16 at 18:05
  • @CommercialSuicide: So then what don't you understand about it? What exactly do you want to know? "Help?" is not a question. – Bergi Jul 17 '16 at 18:08
  • @Bergi It's NOT MY question) – P.S. Jul 17 '16 at 18:09
  • @Bergi how does these messageForRahul/Greg gets executed inside the inner function or it calls the inner function by parameter message? – glendon philipp baculio Jul 17 '16 at 19:25
  • @CommercialSuicide: Oops, but then I don't understand your comment. Of course every code does what it is written to do (disregarding compiler/interpreter bugs), but something must have been unexpected/unclear to the OP. – Bergi Jul 17 '16 at 20:22
  • @glendonphilippbaculio: `messageForRahul` *is* the inner function that got returned. You could have equally declared it and then returned it by name. – Bergi Jul 17 '16 at 20:24

1 Answers1

1

I'll try to give you the basic understanding.

var messageForRahul = sendMessageTo("Rahul");

You create a variable that invokes the function sendMessageTo and passes to it the parameter ("Rahul"). Than you do the same thing with this line of code: var messageForGreg = sendMessageTo("Greg");

Now you have two independent functions, which were running with different parameters.

The line messageForRahul("Hello, Rahul"); runs your inner function for function in which you passed the parameter ("Rahul").

As you can imagine, the last line of code runs your inner function for another function in which you passed the parameter ("Greg").

This code should help you to understand, what's happening. You can write the code this way:

    var sendMessageTo = function(name) {
      return function(message) {
        console.log("Message for " + name + ": " + message);
      }
    };
    sendMessageTo("Rahul")("Hello, Rahul");
    sendMessageTo("Greg")("Hello, Greg");

First you invoke the outer function sendMessageTo("Rahul"), and then the inner function ("Hello, Rahul").

P.S.
  • 15,970
  • 14
  • 62
  • 86
  • My english is a little confusing, but i hope you will get the main line. And feel free to improve this answer. – P.S. Jul 17 '16 at 17:26
  • yes, that's what I don't understand, How does it get into/ get executed in the inner function scope? – glendon philipp baculio Jul 17 '16 at 19:17
  • @glendon philipp baculio I improved the answer, check this. And feel free to ask, if i didn't answer your question yet. – P.S. Jul 17 '16 at 19:29
  • I was confused because they were not declared as a parameter.inside the inner function – glendon philipp baculio Jul 17 '16 at 19:30
  • `function(name)` in also a declaration of `var name` in the **scope** of the (this time anonymous) **function**. Read [Closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures). It is really interesting. – Alex Kudryashev Jul 17 '16 at 19:54