JS Closure Learning Notes (1): What is closure

Source: Internet
Author: User
Tags closure

Closures: When a function is executed outside its declared scope environment, it can remember and use variables in its original scope.

function foo () {
    var a = 2;
    function Bar () {
        console.log (a);
    }
    return bar;
}

var baz = foo ();
Baz (); 2

The bar function can use variables within the Foo function. And when the last Baz function executes, it is actually a bar function, and the bar function is apparently invoked outside of its declared function, but the final output is 2.

It may be generally felt that after the Foo function has finished executing, its internal scope disappears (according to the JS garbage collection mechanism). But because of the closure, Foo's internal scope is still there, and the bar function still has a reference to this scope, and that reference is the closure .

So when Baz executes (actually bar execution), it can still use the scope environment when it is declared, so you can use variable a.

Similarly, there are a number of situations where functions are passed as arguments, when the function is likely to be executed in another location. Such as:

function foo () {
    var a = 2;
    function Baz () {
        console.log (a);
    }
    Bar (baz);
}

Function Bar (FN) {
    fn ();//The actual call is the inner function of Foo, which is the closure
}
VAR fn;
function foo () {
    var a = 2;
    function Baz () {
        console.log (a);
    }
    fn = Baz; The Baz was passed to a global variable
}

function Bar () {
    fn ();//The actual call is the inner function of Foo, which is the closure
}

foo ();
Bar (); 2

regardless of how the internal function is passed to the outside, it maintains a reference to the scope environment when it was originally declared, and the closure is executed regardless of where the function is invoked.

Look at an example:

function Wait (message) {
    settimeout (function timer () {
        console.log (message);
    },1000);
}

Wait ("Hello closure");

The internal function timer acts as a settimeout parameter, and the lexical scope of the intrinsic function contains the scope of the external wait function, so the timer keeps a reference to the variable message.

Executes the wait function, and the asynchronous internal function timer uses the closure to continue using the variable message when the 1000 milliseconds pass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.