How nested function access variables in Javascript at a later time without declaration?












0















In below code how the parameters passed to 'DPerson' function survive even after call to this function has ended successfully. If this is related to how stack and heaps work, can anyone explain it in more detail with respect to this example.



I was of the understanding that, a 'var name = "someValue"' property need to be created in at least one of the execution contexts to print something in console log. But apparently my understanding is wrong.






function DPerson(name, age, job) {
var o = new Object();

o.sayName = function() {
console.log(name);
}

return o;
}

var dperson1 = new DPerson("Ahu", 55, "Wild life expert");
dperson1.sayName();












share|improve this question

























  • The concept you're looking for is called a closure :)

    – Olian04
    Nov 15 '18 at 23:32






  • 1





    In this particular code only name will "survive" - not age or job. And the reason it does survive is because the sayName method of the returned object has a "closure" over the name variable. There are probably thousands of good explanations online of how closure works in Javascript, I suggest you read some if you're confused by this.

    – Robin Zigmond
    Nov 15 '18 at 23:33
















0















In below code how the parameters passed to 'DPerson' function survive even after call to this function has ended successfully. If this is related to how stack and heaps work, can anyone explain it in more detail with respect to this example.



I was of the understanding that, a 'var name = "someValue"' property need to be created in at least one of the execution contexts to print something in console log. But apparently my understanding is wrong.






function DPerson(name, age, job) {
var o = new Object();

o.sayName = function() {
console.log(name);
}

return o;
}

var dperson1 = new DPerson("Ahu", 55, "Wild life expert");
dperson1.sayName();












share|improve this question

























  • The concept you're looking for is called a closure :)

    – Olian04
    Nov 15 '18 at 23:32






  • 1





    In this particular code only name will "survive" - not age or job. And the reason it does survive is because the sayName method of the returned object has a "closure" over the name variable. There are probably thousands of good explanations online of how closure works in Javascript, I suggest you read some if you're confused by this.

    – Robin Zigmond
    Nov 15 '18 at 23:33














0












0








0








In below code how the parameters passed to 'DPerson' function survive even after call to this function has ended successfully. If this is related to how stack and heaps work, can anyone explain it in more detail with respect to this example.



I was of the understanding that, a 'var name = "someValue"' property need to be created in at least one of the execution contexts to print something in console log. But apparently my understanding is wrong.






function DPerson(name, age, job) {
var o = new Object();

o.sayName = function() {
console.log(name);
}

return o;
}

var dperson1 = new DPerson("Ahu", 55, "Wild life expert");
dperson1.sayName();












share|improve this question
















In below code how the parameters passed to 'DPerson' function survive even after call to this function has ended successfully. If this is related to how stack and heaps work, can anyone explain it in more detail with respect to this example.



I was of the understanding that, a 'var name = "someValue"' property need to be created in at least one of the execution contexts to print something in console log. But apparently my understanding is wrong.






function DPerson(name, age, job) {
var o = new Object();

o.sayName = function() {
console.log(name);
}

return o;
}

var dperson1 = new DPerson("Ahu", 55, "Wild life expert");
dperson1.sayName();








function DPerson(name, age, job) {
var o = new Object();

o.sayName = function() {
console.log(name);
}

return o;
}

var dperson1 = new DPerson("Ahu", 55, "Wild life expert");
dperson1.sayName();





function DPerson(name, age, job) {
var o = new Object();

o.sayName = function() {
console.log(name);
}

return o;
}

var dperson1 = new DPerson("Ahu", 55, "Wild life expert");
dperson1.sayName();






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 2:20









Olian04

2,18511136




2,18511136










asked Nov 15 '18 at 23:23









MohitMohit

15318




15318













  • The concept you're looking for is called a closure :)

    – Olian04
    Nov 15 '18 at 23:32






  • 1





    In this particular code only name will "survive" - not age or job. And the reason it does survive is because the sayName method of the returned object has a "closure" over the name variable. There are probably thousands of good explanations online of how closure works in Javascript, I suggest you read some if you're confused by this.

    – Robin Zigmond
    Nov 15 '18 at 23:33



















  • The concept you're looking for is called a closure :)

    – Olian04
    Nov 15 '18 at 23:32






  • 1





    In this particular code only name will "survive" - not age or job. And the reason it does survive is because the sayName method of the returned object has a "closure" over the name variable. There are probably thousands of good explanations online of how closure works in Javascript, I suggest you read some if you're confused by this.

    – Robin Zigmond
    Nov 15 '18 at 23:33

















The concept you're looking for is called a closure :)

– Olian04
Nov 15 '18 at 23:32





The concept you're looking for is called a closure :)

– Olian04
Nov 15 '18 at 23:32




1




1





In this particular code only name will "survive" - not age or job. And the reason it does survive is because the sayName method of the returned object has a "closure" over the name variable. There are probably thousands of good explanations online of how closure works in Javascript, I suggest you read some if you're confused by this.

– Robin Zigmond
Nov 15 '18 at 23:33





In this particular code only name will "survive" - not age or job. And the reason it does survive is because the sayName method of the returned object has a "closure" over the name variable. There are probably thousands of good explanations online of how closure works in Javascript, I suggest you read some if you're confused by this.

– Robin Zigmond
Nov 15 '18 at 23:33












2 Answers
2






active

oldest

votes


















2














What you are seeing here is a closure. When you define a function inside another function, the child function has access to the lexical environment of the parent and keeps that even after the parent has returned. name is defined in the parent when you create the parameters to DPerson. The child function, o.sayName has access to this and retains access to it after the parent returns.



See MDN - closures for more






share|improve this answer































    -1














    Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329266%2fhow-nested-function-access-variables-in-javascript-at-a-later-time-without-decla%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      What you are seeing here is a closure. When you define a function inside another function, the child function has access to the lexical environment of the parent and keeps that even after the parent has returned. name is defined in the parent when you create the parameters to DPerson. The child function, o.sayName has access to this and retains access to it after the parent returns.



      See MDN - closures for more






      share|improve this answer




























        2














        What you are seeing here is a closure. When you define a function inside another function, the child function has access to the lexical environment of the parent and keeps that even after the parent has returned. name is defined in the parent when you create the parameters to DPerson. The child function, o.sayName has access to this and retains access to it after the parent returns.



        See MDN - closures for more






        share|improve this answer


























          2












          2








          2







          What you are seeing here is a closure. When you define a function inside another function, the child function has access to the lexical environment of the parent and keeps that even after the parent has returned. name is defined in the parent when you create the parameters to DPerson. The child function, o.sayName has access to this and retains access to it after the parent returns.



          See MDN - closures for more






          share|improve this answer













          What you are seeing here is a closure. When you define a function inside another function, the child function has access to the lexical environment of the parent and keeps that even after the parent has returned. name is defined in the parent when you create the parameters to DPerson. The child function, o.sayName has access to this and retains access to it after the parent returns.



          See MDN - closures for more







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 23:32









          Mark MeyerMark Meyer

          39.5k33162




          39.5k33162

























              -1














              Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.






              share|improve this answer




























                -1














                Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.






                share|improve this answer


























                  -1












                  -1








                  -1







                  Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.






                  share|improve this answer













                  Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 23:37









                  Edwin Dijas ChiwonaEdwin Dijas Chiwona

                  35129




                  35129






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329266%2fhow-nested-function-access-variables-in-javascript-at-a-later-time-without-decla%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues