One of the most common questions you’ll be asked in an interview is what is a closure? The answer to this question is not only to understand the concept, but also to understand when we use closures in our daily development so that we can be consistent with what we know and what we do. The following is my understanding of this question. I will ask questions to get a deeper understanding.

What is a closure?

After reading a lot of articles and materials, I found what I thought was the most general explanation “closure is a function that has access to a variable in the scope of another function”, quoted from javascript Advanced Programming, through which we can get the following understanding and question:

  • Closures are a special case function.
  • What is scope?
  • Why do closures have access to variables of other functions?
  • What special case can lead to closures?
  • Where are closures used in real development?

Before answering these questions, let’s show the closure in code:

var gender
function person(){
    var name = 'leo'
    console.log(gender)
    return function(){
        var gender = 'male'console.log(name) } } var sayName = person(); // Print undefined because gender is not available in the person function The function returned is the closure sayNmae() // prints Leo // By definition since sayName can access variables in the Person function, // all sayName functions are closuresCopy the code

What is scope?

In this example, we can see that the function name can be accessed by executing the sayName function, but the third line of code is undefined. We can see that it prints the variables defined in the first line instead of gender in the sixth line. It can be seen that the ranges of variables are different in different functions. When I look up the quality, I see that the scope is created when the execution context is created, so to understand the scope, we need to understand the following question.

What is an execution context?

Execution context is the function of the environment, or the code above, first of all, as long as where can run js (for example, we open a TAB page of the chrom), will be the first to implement a main function, after the execution, it creates a js execution environment, the environment will be some one can access the object is the window, We call this execution environment the execution context. When we create the execution context, we create an object associated with the context, which contains the scope chain and variables in the function. Each execution context created combines the current scope with the scope of the function enclosing the current function, creating a scope chain.

What is an execution stack?

Deposit is the execution context of a stack structure, we all know that js is single-threaded, and perform only one task at a time, at the time of executing code is the same, as all will be taken in the execution stack code execution context, every time after the execution will make the operation of the stack, if, no was quoted as saying in the context of a variable will destroy the execution context.

To sum up

I think all functions in JS are actually closures, because THERE must be a global execution context in JS, and as long as the browser is not closed, the execution environment must exist, and the variables in this, we can access in other functions, such as window. As mentioned in the opening paragraph, “closures are functions that have access to variables in the scope of another function.” The above is my personal opinion, welcome to testify.