The interviewer asked, what is a closure and how should I answer it?

the interviewer asks, what is a closure and how should I answer it?
I want to answer a paragraph or definition, not a function instance

Jun.20,2022

recommended elevation, online understanding is personal, you can refer to, but have their own understanding is the best.

I think the better answer is:

1. Closure conditions: a, outer function nesting inner function; b, inner function referencing variables in outer function; c, outer function returning inner function;
2, js garbage collection mechanism: when a variable is not referenced, it is recycled
3. When each function is declared, an active object containing variables within the scope of the function is generated

give a simple example to analyze:

function a() {
    var x = 1
    var y = 100
    console.log(y)
    yPP
    return function b() {
        console.log(xPP)
    }
}
var result= a() // 100
var result1 = a() // 100
-----------------------------
result() // 1
result() // 2

in the above example, when there is no closure, I destroy the function scope after I execute a, then x is also destroyed, so the result of two execution should be twice 1. But the second time is 2, which means that the internal x has not been destroyed, that is, the x in the scope of a function has been saved.

starts the analysis with the above three points: when the
a function is declared, an active object containing the variables within the scope of the a function is created, which can be called the local parameter x inside the aa,aa; when the
b function is declared, the x in aa is referenced; after the
a function is executed, the global variable result refers to the b function.
the garbage collection mechanism combined with JS, x references will not be destroyed, b references will not be destroyed, result is a global variable will not be destroyed, x can be retained, thus forming a closure. Because the y in aa is not referenced, it is destroyed.

above is my understanding. Finally, I can answer the application of closures. I won't answer them here. There are many

on the Internet.

to sum up, the suggested answer is to add the above three points to the application


closures are functions that have access to variables in the scope of another function. The "JavaScript Advanced programming"


closure is a function nested function, and variables outside the function can be used inside the function.


A function that can access variables of an external function, for example:

function a(x){
      var z=3;
      function b(y){  // axz
          console.log(x+y+(PPz));
     }
    return b;
}

the first-level function calls the second-level function, and the second-level function accesses the first-level function lexical scope variable is called closure, and the simpler point is the function nest function


remember which book you read, and the best way to understand closures is to compare it with the concept of class :

  • The class is the data with methods.
  • closure is a method with data.

explanation: the closure is formed when the current function can access variables in other scopes.
function:
1. Expand the scope of variables
2. Prevent variables from being destroyed (garbage collection mechanism)


define function a , function b
satisfy:

  • Variables within the scope of the function b are referenced in the scope of the function a . (a captures the free variable of b)
  • The function a can be executed outside the scope of the function b . (the variable object of b cannot be destroyed normally)

forms a closure .
how to answer each person's expression, just mean it.

// 
function b(){
    var z = 3;
    function a(y){
        console.log(z + y);  // a  b z
    }
    return a;
}
var c = b();
c(1);  // a  b 

popularly speaking: there is a function in the function; further understanding: the wrapped function can access the variables of the wrapped function;


after looking at all the answers, everyone's understanding is the same, except for one thing: does the closure have to be accessed by the outside world?
the answer is: No!

function a(x){
      var z=3;
      function b(y){  // axz
          console.log(x+y+(PPz));
     }
    return b;
}

is it still a closure to remove return?
the answer is: Yes!
it is both an internal function and a closure, and the two do not conflict.

definition of closures on mdn:

A
closure is a combination of a function and the lexical environment in which it is declared.

as long as it is satisfied, this is a function, which uses external variables inside, and it is a closure. The first example on
mdn:

function init() {
    var name = "Mozilla"; // name  init 
    function displayName() { // displayName() ,
        alert(name); // 
    }
    displayName();
}
init();

because the diaplayName cannot be accessed again, the name in the closure is also recycled.
by definition, displayName is a closure, although it has no use for eggs.

-split line--

in addition, by definition, any function can be a closure in a broad sense, because external references in the lexical environment may be empty.
so if the interviewer asks you about closures, I suggest you ask him first whether to answer the definition of closures or most practices, in a narrow sense or in a broad sense.


I think this is not bad. I don't quite understand closures either. https://codeshelper.com/a/11...


< H2 > personal opinion < / H2 >
  • closures have been used by many people from the beginning to the end, but they are not recommended until now, which will cause logic confusion

function nesting function

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b387b3-2c146.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b387b3-2c146.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?