The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we help you to solve your academic and programming questions fast.
In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE
See Our team
Wondering how we keep quality?
Got unsolved questions? Ask Questions

Javascript-Interview-questions-->View question

What you mean by closers in javascript? - javascript in

What you mean by closers in javascript? - javascript in And How does the same works?
What you mean by closers in javascript? - javascript in And How does the same works?


Asked On2021-10-09 13:08:52 by:sharma

Taged users:


Likes:
Be first to like this question

Dislikes:
Be first to dislike this question
Talk about this  Like  Dislike
View all qusetions
Answers
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

Consider the following example
function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));  // 7 
console.log(add10(2)); // 12
add5 and add10 are both closures. They share the same function body definition, but store different lexical environments. In add5's lexical environment, x is 5, while in the lexical environment for add10x is 10.
 Lets Dig deep into these concepts. Execution ContextLexical Environment, and Identifier Resolution This will give us better understanding of what we are actually dealing with.
Execution Context
every time we call a function, a new execution context is created and is put on top of the execution stack. The same pattern follows when you call the nested function which calls another nested function:


Lexical Environment
A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code
Identifier Resolution
Lets check the below snippet of code and try to understand what is an identifier resolution from the same.
var x = 10;
function foo(){
var y = 20;
console.log(x+y); // 30
}
// Environment technically consist of two main components:
// environmentRecord, and a reference to the outer environment
// Environment of the global context
globalEnvironment = {
environmentRecord: {
// built-ins
// our bindings:
x: 10
},
outer: null // no parent environment
};
// Environment of the "foo" function
fooEnvironment = {
environmentRecord: {
y: 20
},
outer: globalEnvironment
};

As you can see when trying to resolve the identifier “x” in the foo context, the outer environment (global) is reached out. This process is called identifier resolution and happens on running execution context.
This will give us better understanding of what we are actually dealing with.






Answerd on:2021-10-10 Answerd By:batsam22

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:Watch more here

Watch more videos from this user Here

Learn how to upload a video over here



Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder