closure

  1. concept

    A function that has access to variables in the scope of another function

  2. example

    function books() {
        var books = "Books in my bag";
        return function() {
            console.log(books); }}var bag = books();
    bag();
    Copy the code
    1. The first element of the array points to the global variable object, which contains variables and function names that have not yet been assigned values. If it is a function, a function execution environment is created that also has a chain of scopes

    2. Before executing the books() function, a books function variable object is created and placed at the top of the scoped chain array. (The scoped link of the anonymous function’s execution environment inherits the scoped chain of the execution environment in which it was created.)

    3. Before executing an anonymous function, create an anonymous function variable object and place it at the top of the list of anonymous function scoped chains

    4. After books() is executed, the books/bag function’s scope chain is destroyed. The books/bag variable object is not destroyed in the presence of the anonymous function’s scope chain.