In-depth understanding of closure series The first-what is the closure of the package

Source: Internet
Author: User

Previous words

Closures have become an almost mythical concept, very important and difficult to master, and difficult to define. In this paper, we start with the definition of closure

Ancient definition

Closure (closure), refers to function variables can be stored in the function scope, so it appears that the function of the variable "wrapped" up

So, the function that contains the variable is the closure

// According to the old definition, the function foo that contains the variable n is the closure function foo () {    var n = 0;} Console.log (n)//uncaught referenceerror:n is not defined

Define a

A closure is a function that can access the scope of which it is located

So, the function that needs to find the variable through the scope chain is the closure

// By definition, the function foo () that needs to find the variable n in the global environment through the scope chain is the closure var n = 0; function foo () {    console.log (n)//0}foo ();

Define Two

Closures refer to functions that have access to variables in another function scope

So, the inner function that accesses the scope of the upper function is the closure

// according to definition two, the bar function nested in the Foo function is the closure function foo () {    var a = 2;     function Bar () {        //  2    }    Bar ();} Foo ();

Define Three

Closures are functions that are called outside the scope of the function declaration

Calling a function outside the scope of the function declaration requires that the function be passed as a return value or as a parameter

"1" return value

// according to definition three, declare in the scope of the Foo () function that the bar () function called in the scope of the global environment is a closure function foo () {    var a = 2;     function Bar () {        //2    }    return  bar;} Foo () ();

"2" parameter

// By definition three, the Baz () function called in the scope of the bar () function is declared in the scope of the Foo () function, which is a closure function foo () {    var a = 2;     function Baz () {        //2    }    Bar (baz);} function Bar (FN) {    fn ();}

Thus, regardless of the means by which an intrinsic function is passed to the lexical scope in which it resides, it holds a reference to the original scope, regardless of where the function is executed, using closures

Iife

Iife, is it a closed bag?

The Foo () function is defined in the global scope and is immediately called in the global scope, if it is a closure by definition. If you follow definition two and define three, it's not a closure.

var a = 2;(function  foo () {    console.log (a); // 2}) ();

At last

The definition of closures is confusing, and I think it is related to the different interpretations of classic books. The classic definition is the exact words of the Rhino book, the definition of the elevation.

But, to sum up is about a function to be a closure in the end need to satisfy a few conditions

Strictly speaking, closures need to meet three conditions: "1" Access scope; "2" function nesting; "3" is called outside the scope of the action

Some people think that only meet the conditions of 1, so Iife is a closure, some people feel satisfied with the conditions 1 and 2 can be, so the nested function is the closure; some people think that 3 conditions are satisfied, so the function that is called outside the scope is the closure

The question is, who is the authority?

In-depth understanding of closure series The first-what is the closure of the package

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.