JavaScript closures (Closure)

Source: Internet
Author: User
Tags variable scope

The scope of a variable
To understand the closure, you should first understand the JS special variable scope.
There are two types of variables scoped to JS: Global variables and local variables.

Global variables:

var a = 999;function F1 () {alert (a);} F1 (); 999

The variable A is a global variable, so the function F1 is a local variable, and the local variable can get to the global variable.

Local variables:

Function F1 () {var a = 999;} alert (a);//undefined

Variable A is created in the function as a local variable, so local variables cannot be obtained globally.

Global variables:

Function F1 () {a = 999;} alert (a);//999

Variable A has no Var, so a is a global variable.

Second, how to read the local variables from the outside?
For a variety of reasons, we sometimes need to get local variables within the function. However, as already mentioned, under normal circumstances, this can not be done, only through the workaround to achieve.
That is, in the inside of the function, define a function.

Function F1 () {var n=999; function F2 () {alert (n);//999}}

The function F2 is F1 included, so the F1 variable is visible to the F2, and vice versa, the F2 variable is invisible to F1. This is the "chained scope" (chain scope) in Javasript, and the child objects are always looked up into the parent object, so the parent object's variable is visible to the object.
Since F2 can read the local variables in the F1, we can not read its internal variables outside the F1 as long as the F2 is the return value!

Function F1 () {var n=999;     function F2 () {alert (n);  } return F2;  } var result=f1 (); Result (); 999


Three, the concept of closure
The F2 function in the previous section of the code is the closure.
The definition of "closure" (closure) in various professional literature is very abstract and difficult to read. My understanding is that closures are functions that can read other functions ' internal variables.
Because in the JavaScript language, only sub-functions inside the function can read local variables, it is possible to simply interpret the closure as "a function defined inside a function".
So, in essence, a closure is a bridge that connects the inside of the function to the outside of the function.

Iv. use of closures
Closures can be used in many places. Its maximum usefulness is two, one of the previously mentioned variables that can read the inside of a function, and the other is to keep the values of these variables in memory at all times.
How to understand this sentence? Take a look at the following code.

Function F1 () {var n=999;    Nadd=function () {n+=1} function F2 () {alert (n);  } return F2;  } var result=f1 (); Result ();  999 Nadd (); Result (); 1000

In this code, result is actually the closure F2 function. It runs altogether two times, the first value is 999, the second value is 1000. This proves that the local variable n in the function F1 is kept in memory and is not automatically cleared after the F1 call.
Why is that? The reason is that F1 is the parent function of F2, and F2 is assigned to a global variable, which causes F2 to always be in memory, and F2 's presence depends on F1, so F1 is always in memory and will not be reclaimed by the garbage collection mechanism (garbage collection) after the call ends.
Another notable part of this code is the line "nadd=function () {n+=1}", which first did not use the var keyword in front of Nadd, so Nadd is a global variable, not a local variable. Second, the value of Nadd is an anonymous function (anonymous functions), and the anonymous function itself is a closure, so nadd is equivalent to a setter that can manipulate local variables inside the function outside of the function.

V. Note points for using closures
1) Because the closure will make the variables in the function are stored in memory, memory consumption is very large, so can not abuse closures, otherwise it will cause the performance of the Web page, in IE may cause memory leaks. The workaround is to remove all unused local variables before exiting the function.
2) The closure changes the value of the inner variable of the parent function outside the parent function. So, if you use the parent function as an object, and the closure as its public method, and the internal variable as its private property (private value), be careful not to arbitrarily change the value of the inner variable of the parent function.

Liu, study questions
If you can understand the results of the following two sections of code, you should understand the operation mechanism of the closure.
Code snippet one.

var name = "the window";      var object = {Name: "My Object", Getnamefunc:function () {return function () {return;    };  }  }; Alert (Object.getnamefunc () ());

Code snippet two.

var name = "the window";      var object = {Name: "My Object", Getnamefunc:function () {var = this;      return function () {return;    };  }  }; Alert (Object.getnamefunc () ());


JavaScript closures (Closure)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.