Javascript - How does this preservation of variable works in Closure javascript? [closed]

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-3
down vote
favorite
function outside(x)
function inside(y)
return x + y;
return inside;
fn_inside = outside(3); //1* - Think of it like: give me a function that adds 3 to whatever you give it
result = fn_inside(5); //2* - returns 8
result1 = outside(3)(5); // returns 8
1*,2* - Since each call provides potentially different arguments, a new closure is created for each call to outside. The memory can be freed only when the returned inside is no longer accessible.
Questions
1) What happens when we call outside(3)?
- 1a) What does it return?
2) Is fn_inside(3); a valid function call?
3) How is outside(3)(5) valid when outside function definition
accepts only one parameter?
javascript closure
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t Jun 27 at 10:12
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t
add a comment |Â
up vote
-3
down vote
favorite
function outside(x)
function inside(y)
return x + y;
return inside;
fn_inside = outside(3); //1* - Think of it like: give me a function that adds 3 to whatever you give it
result = fn_inside(5); //2* - returns 8
result1 = outside(3)(5); // returns 8
1*,2* - Since each call provides potentially different arguments, a new closure is created for each call to outside. The memory can be freed only when the returned inside is no longer accessible.
Questions
1) What happens when we call outside(3)?
- 1a) What does it return?
2) Is fn_inside(3); a valid function call?
3) How is outside(3)(5) valid when outside function definition
accepts only one parameter?
javascript closure
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t Jun 27 at 10:12
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t
Welcome to Code Review. This question doesn't reflect what the site is about. It's not on-topic to ask for an explanation of code that you do not understand. See What topics can I ask about?.
â Phrancis
Jun 27 at 6:03
@Phrancis - It says what questions should be asked here. But it doesn't say where this kind of question go to . When I go to Stackoverflow, people kick me out there the same way . So I came here and people do the same. Please take this issue seriously and come up with a tabular column or AI to recognise and categorise the question. It would definitely help the community.
â user3705478
Jun 27 at 9:06
Your question definitely does not fit Code Review, and stack overflow is to help with bugs, hence your question does not fit them either. You could try dreamincode.net/forums/forum/90-javascript but I will warn you your question looks a lot like homework and thus you will have a hard time getting an answer at most sites. You can always open a browser and run the code you have and that will answer your first two questions. And a hint for 3, Its valid becauseoutside(3)returns a function that is called with the second(5)Its 2 calls, not one call with 2 args
â Blindman67
Jun 27 at 9:57
@Blindman67 Thank you very much for answering. Oh my god. I'm 27 years old and I'm not working on homework. If I add my personal profile details as Engineer, will people help here. I seem to keep facing the issue of posting correct question in correct part of StackExchange.
â user3705478
Jun 27 at 22:52
add a comment |Â
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
function outside(x)
function inside(y)
return x + y;
return inside;
fn_inside = outside(3); //1* - Think of it like: give me a function that adds 3 to whatever you give it
result = fn_inside(5); //2* - returns 8
result1 = outside(3)(5); // returns 8
1*,2* - Since each call provides potentially different arguments, a new closure is created for each call to outside. The memory can be freed only when the returned inside is no longer accessible.
Questions
1) What happens when we call outside(3)?
- 1a) What does it return?
2) Is fn_inside(3); a valid function call?
3) How is outside(3)(5) valid when outside function definition
accepts only one parameter?
javascript closure
function outside(x)
function inside(y)
return x + y;
return inside;
fn_inside = outside(3); //1* - Think of it like: give me a function that adds 3 to whatever you give it
result = fn_inside(5); //2* - returns 8
result1 = outside(3)(5); // returns 8
1*,2* - Since each call provides potentially different arguments, a new closure is created for each call to outside. The memory can be freed only when the returned inside is no longer accessible.
Questions
1) What happens when we call outside(3)?
- 1a) What does it return?
2) Is fn_inside(3); a valid function call?
3) How is outside(3)(5) valid when outside function definition
accepts only one parameter?
javascript closure
asked Jun 27 at 5:11
user3705478
972
972
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t Jun 27 at 10:12
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t
closed as off-topic by ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t Jun 27 at 10:12
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â ÃÂìýÃÂñ á¿¥Ã栨Â, Billal BEGUERADJ, Phrancis, Ludisposed, t3chb0t
Welcome to Code Review. This question doesn't reflect what the site is about. It's not on-topic to ask for an explanation of code that you do not understand. See What topics can I ask about?.
â Phrancis
Jun 27 at 6:03
@Phrancis - It says what questions should be asked here. But it doesn't say where this kind of question go to . When I go to Stackoverflow, people kick me out there the same way . So I came here and people do the same. Please take this issue seriously and come up with a tabular column or AI to recognise and categorise the question. It would definitely help the community.
â user3705478
Jun 27 at 9:06
Your question definitely does not fit Code Review, and stack overflow is to help with bugs, hence your question does not fit them either. You could try dreamincode.net/forums/forum/90-javascript but I will warn you your question looks a lot like homework and thus you will have a hard time getting an answer at most sites. You can always open a browser and run the code you have and that will answer your first two questions. And a hint for 3, Its valid becauseoutside(3)returns a function that is called with the second(5)Its 2 calls, not one call with 2 args
â Blindman67
Jun 27 at 9:57
@Blindman67 Thank you very much for answering. Oh my god. I'm 27 years old and I'm not working on homework. If I add my personal profile details as Engineer, will people help here. I seem to keep facing the issue of posting correct question in correct part of StackExchange.
â user3705478
Jun 27 at 22:52
add a comment |Â
Welcome to Code Review. This question doesn't reflect what the site is about. It's not on-topic to ask for an explanation of code that you do not understand. See What topics can I ask about?.
â Phrancis
Jun 27 at 6:03
@Phrancis - It says what questions should be asked here. But it doesn't say where this kind of question go to . When I go to Stackoverflow, people kick me out there the same way . So I came here and people do the same. Please take this issue seriously and come up with a tabular column or AI to recognise and categorise the question. It would definitely help the community.
â user3705478
Jun 27 at 9:06
Your question definitely does not fit Code Review, and stack overflow is to help with bugs, hence your question does not fit them either. You could try dreamincode.net/forums/forum/90-javascript but I will warn you your question looks a lot like homework and thus you will have a hard time getting an answer at most sites. You can always open a browser and run the code you have and that will answer your first two questions. And a hint for 3, Its valid becauseoutside(3)returns a function that is called with the second(5)Its 2 calls, not one call with 2 args
â Blindman67
Jun 27 at 9:57
@Blindman67 Thank you very much for answering. Oh my god. I'm 27 years old and I'm not working on homework. If I add my personal profile details as Engineer, will people help here. I seem to keep facing the issue of posting correct question in correct part of StackExchange.
â user3705478
Jun 27 at 22:52
Welcome to Code Review. This question doesn't reflect what the site is about. It's not on-topic to ask for an explanation of code that you do not understand. See What topics can I ask about?.
â Phrancis
Jun 27 at 6:03
Welcome to Code Review. This question doesn't reflect what the site is about. It's not on-topic to ask for an explanation of code that you do not understand. See What topics can I ask about?.
â Phrancis
Jun 27 at 6:03
@Phrancis - It says what questions should be asked here. But it doesn't say where this kind of question go to . When I go to Stackoverflow, people kick me out there the same way . So I came here and people do the same. Please take this issue seriously and come up with a tabular column or AI to recognise and categorise the question. It would definitely help the community.
â user3705478
Jun 27 at 9:06
@Phrancis - It says what questions should be asked here. But it doesn't say where this kind of question go to . When I go to Stackoverflow, people kick me out there the same way . So I came here and people do the same. Please take this issue seriously and come up with a tabular column or AI to recognise and categorise the question. It would definitely help the community.
â user3705478
Jun 27 at 9:06
Your question definitely does not fit Code Review, and stack overflow is to help with bugs, hence your question does not fit them either. You could try dreamincode.net/forums/forum/90-javascript but I will warn you your question looks a lot like homework and thus you will have a hard time getting an answer at most sites. You can always open a browser and run the code you have and that will answer your first two questions. And a hint for 3, Its valid because
outside(3) returns a function that is called with the second (5) Its 2 calls, not one call with 2 argsâ Blindman67
Jun 27 at 9:57
Your question definitely does not fit Code Review, and stack overflow is to help with bugs, hence your question does not fit them either. You could try dreamincode.net/forums/forum/90-javascript but I will warn you your question looks a lot like homework and thus you will have a hard time getting an answer at most sites. You can always open a browser and run the code you have and that will answer your first two questions. And a hint for 3, Its valid because
outside(3) returns a function that is called with the second (5) Its 2 calls, not one call with 2 argsâ Blindman67
Jun 27 at 9:57
@Blindman67 Thank you very much for answering. Oh my god. I'm 27 years old and I'm not working on homework. If I add my personal profile details as Engineer, will people help here. I seem to keep facing the issue of posting correct question in correct part of StackExchange.
â user3705478
Jun 27 at 22:52
@Blindman67 Thank you very much for answering. Oh my god. I'm 27 years old and I'm not working on homework. If I add my personal profile details as Engineer, will people help here. I seem to keep facing the issue of posting correct question in correct part of StackExchange.
â user3705478
Jun 27 at 22:52
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
outside(3)returns a function. Like you said, it returns a function takes one argumentnand returnsn + 3. Mathematically, this might be written as f(n) = n + 3.fn_insideis a binding made to the return value ofoutside(3), which we know, from part 1, is a function. So when we callfn_inside(3), it's like plugging in 3 for n: f(3) = 3 + 3 = 6. The return value is simply a number: 6.outside(3)(5)is equivalent to(outside(3))(5)and recall thatoutside(3)is a function that takes one argument.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
outside(3)returns a function. Like you said, it returns a function takes one argumentnand returnsn + 3. Mathematically, this might be written as f(n) = n + 3.fn_insideis a binding made to the return value ofoutside(3), which we know, from part 1, is a function. So when we callfn_inside(3), it's like plugging in 3 for n: f(3) = 3 + 3 = 6. The return value is simply a number: 6.outside(3)(5)is equivalent to(outside(3))(5)and recall thatoutside(3)is a function that takes one argument.
add a comment |Â
up vote
1
down vote
accepted
outside(3)returns a function. Like you said, it returns a function takes one argumentnand returnsn + 3. Mathematically, this might be written as f(n) = n + 3.fn_insideis a binding made to the return value ofoutside(3), which we know, from part 1, is a function. So when we callfn_inside(3), it's like plugging in 3 for n: f(3) = 3 + 3 = 6. The return value is simply a number: 6.outside(3)(5)is equivalent to(outside(3))(5)and recall thatoutside(3)is a function that takes one argument.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
outside(3)returns a function. Like you said, it returns a function takes one argumentnand returnsn + 3. Mathematically, this might be written as f(n) = n + 3.fn_insideis a binding made to the return value ofoutside(3), which we know, from part 1, is a function. So when we callfn_inside(3), it's like plugging in 3 for n: f(3) = 3 + 3 = 6. The return value is simply a number: 6.outside(3)(5)is equivalent to(outside(3))(5)and recall thatoutside(3)is a function that takes one argument.
outside(3)returns a function. Like you said, it returns a function takes one argumentnand returnsn + 3. Mathematically, this might be written as f(n) = n + 3.fn_insideis a binding made to the return value ofoutside(3), which we know, from part 1, is a function. So when we callfn_inside(3), it's like plugging in 3 for n: f(3) = 3 + 3 = 6. The return value is simply a number: 6.outside(3)(5)is equivalent to(outside(3))(5)and recall thatoutside(3)is a function that takes one argument.
answered Jun 27 at 5:47
Samasambo
1454
1454
add a comment |Â
add a comment |Â
Welcome to Code Review. This question doesn't reflect what the site is about. It's not on-topic to ask for an explanation of code that you do not understand. See What topics can I ask about?.
â Phrancis
Jun 27 at 6:03
@Phrancis - It says what questions should be asked here. But it doesn't say where this kind of question go to . When I go to Stackoverflow, people kick me out there the same way . So I came here and people do the same. Please take this issue seriously and come up with a tabular column or AI to recognise and categorise the question. It would definitely help the community.
â user3705478
Jun 27 at 9:06
Your question definitely does not fit Code Review, and stack overflow is to help with bugs, hence your question does not fit them either. You could try dreamincode.net/forums/forum/90-javascript but I will warn you your question looks a lot like homework and thus you will have a hard time getting an answer at most sites. You can always open a browser and run the code you have and that will answer your first two questions. And a hint for 3, Its valid because
outside(3)returns a function that is called with the second(5)Its 2 calls, not one call with 2 argsâ Blindman67
Jun 27 at 9:57
@Blindman67 Thank you very much for answering. Oh my god. I'm 27 years old and I'm not working on homework. If I add my personal profile details as Engineer, will people help here. I seem to keep facing the issue of posting correct question in correct part of StackExchange.
â user3705478
Jun 27 at 22:52