How nested function access variables in Javascript at a later time without declaration?
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();
javascript
add a comment |
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();
javascript
The concept you're looking for is called a closure :)
– Olian04
Nov 15 '18 at 23:32
1
In this particular code onlyname
will "survive" - notage
orjob
. And the reason it does survive is because thesayName
method of the returned object has a "closure" over thename
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
add a comment |
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();
javascript
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
javascript
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 onlyname
will "survive" - notage
orjob
. And the reason it does survive is because thesayName
method of the returned object has a "closure" over thename
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
add a comment |
The concept you're looking for is called a closure :)
– Olian04
Nov 15 '18 at 23:32
1
In this particular code onlyname
will "survive" - notage
orjob
. And the reason it does survive is because thesayName
method of the returned object has a "closure" over thename
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 15 '18 at 23:32
Mark MeyerMark Meyer
39.5k33162
39.5k33162
add a comment |
add a comment |
Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.
add a comment |
Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.
add a comment |
Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.
Yes variables in javascript survive even after the function ended. A function is in short a scope in javascript.
answered Nov 15 '18 at 23:37
Edwin Dijas ChiwonaEdwin Dijas Chiwona
35129
35129
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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" - notage
orjob
. And the reason it does survive is because thesayName
method of the returned object has a "closure" over thename
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