Home  >  Q&A  >  body text

html5 - 用普通人能听懂的话回答什么是闭包

我一般都是说,创建一个动态存储空间,突破作用域的限制。让外部可以访问内部的变量,感觉自己答得没什么问题啊,为什么面试完没通知?

ringa_leeringa_lee2742 days ago1346

reply all(13)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 13:03:28

    In fact, closures are not that advanced and complicated.

    Just from a canonical perspective:

    function a() {
        var x = 1, y = 2;
        return function b() {
            console.log(x + y);
        }
    }
    
    var f = a();
    f();
      During the execution of
    1. , we can think that its local variables are stored in an internal object a, and the execution environment of Env saves a reference to a. Env

    2. When defining

      , b will also save a reference to b. Env

    3. When

      returns, the execution environment of a is destroyed, and its reference to a is gone. However, as long as Env is still accessible (e.g., b returns a via the return value). Then b's reference to b still exists, and it can still access the local variables saved in Env. Env

    4. In the future, during the execution of function

      , both b and will be accessible through the so-called scope chain x. y

    This is the

    closure.

    The diagram represents:

    • Before function

      returns: a

    • After function

      returns: a

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:03:28

    This is such a high-quality answer. If I didn’t receive a notice, it must be because the interviewing unit is not efficient enough

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:03:28

    After your dad gave birth to your son, you can make your own money or use your dad’s money, but you don’t want your dad to use your money.

    Mapped into js.
    A function nests B function. The variables in B function can only be used in the scope generated by B function. A function cannot use the variables of B function. But B can use A and global scope variables.

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:03:28

    is an internal function that references external variables.

    By the way, I’ll post an article I wrote before
    http://scarletsky.github.io/2015/12/02/the-little-javascript-closures/

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:03:28

    A closure is a functional.

    R language is not suitable for JS.

    a <-function () {
         x = 1
         y = 2
         b <- function(){print(x + y)}
        return(b) 
    }
    
    a()()
    
    >[1] 3

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:03:28

    A function that accesses its external variables is a closure

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:03:28

    Callable object.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 13:03:28

    The money in the account is a closure. This is the best explanation I can think of for ordinary people... No one knows what is inside except the account owner (caller) and the bank (memory controller) Do you have money? Although things related to the account are exposed (such as bank cards, but the passbook does not meet the requirements)

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:03:28

    I also encountered this problem during an interview not long ago

    A closure refers to a function that has permission to access variables in another scope.
    Generally, the memory will be released immediately after the function is executed. However, sometimes we need to "remember" the previous execution result each time the function is executed, rather than destroying it immediately. In layman's terms, closures are generally used to extend memory

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:03:28

    Seen on the Internet:
    A closure is actually a nesting of functions. The inner function can use all the variables of the outer function, even if the outer function has been executed.

    function checkClosure() { //外层函数
        var str = 'China'; //局部变量
        setTimeout(
            function() { console.log(str); } //这是一个匿名函数,可以调用外层函数的变量str
        , 2000);
    }
    checkClosure();
    console.log('Hello'); //先显示Hello,2秒后显示China.

    Local variables declared with var in the function are released after the function is executed. Global variables declared outside the function will always reside in the memory. The execution of the checkClosure function is instantaneous, and a function is created in the function body of checkClosure. The local variable str, str is not released after checkClosure is executed, because the anonymous function in setTimeout has a reference to str. After 2 seconds, the anonymous function in the function body is executed, and str is released.

    Another example is PHP closing the database connection when the request ends:

    $db = new mysqli('127.0.0.1','user','pass','dbname',3306);
    register_shutdown_function(function() use ($db) {
        $db->close();
    });

    The database connection $db is a variable outside the anonymous function function(). The difference between PHP and JS is that PHP needs to use use to explicitly declare the variable $db into the anonymous function.

    reply
    0
  • Cancelreply