Understanding of JavaScript closure function

Source: Internet
Author: User
Tags closure

A closure is a function that accesses a variable in the outer scope of its function, that is, a variable that can call a function in a function outside, in fact, a bridge that connects the inside and outside of a function.
Closure three major features:
1. Function nesting functions
2. Intrinsic functions can access variables of external functions
3. Parameters and variables are not recycled
For beginners more difficult to understand, you can use the following two words to deepen understanding:
1. Closures are functions that have access to variables in another function scope, and the most common way to create closures is to create another function within one function and access it through another function
Local variables of a function. The disadvantage of closures is that resident memory increases memory usage and is prone to memory leaks if used improperly.
2, do not have to struggle in the end how to calculate the closure, in fact, you write every function counts as closures, even if the global function, you access the function outside the global variables, is the embodiment of the closure.

Here, for example, observe the following code:

function A () {    var i = ten;    function B () {        alert (i);    }
return b;} var c = A (); C ();

This code has the following two features:

1. function b is nested inside function A, 2, function a returns function B;

The reference relationship is as follows:

  

So after the execution of var C = A (), the variable C is actually pointing to the function b,b used in the variable i, and then C () will pop up a window to display the value of I (the first time is 1). This code actually creates a closure, why? Because the variable C outside of function A refers to function B in function A, that is:

When function A's internal function B is referenced by a variable outside function A, we create a "closure" that we normally call "closures."

When function B executes, it will be the same as the above steps. Therefore, the scope chain of execution B contains 3 objects: The active object of B, the active object of a, and the Window object, as shown in:

, when a variable is accessed in function B, the search order is:

1. Search for the active object itself first, if present, return if there is no active object that will continue to search for function A, and then find until it is found.

2. If the prototype prototype object exists in function B, it finds its own prototype object after it finds its own active object, and then continues to look for it. This is the variable lookup mechanism in JavaScript.

3. if the entire scope chain cannot be found, the undefined is returned.

Use of closures

1. The closure can read the function internal variable 2. Always keep the value of a function's internal variable in memory

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. Closures will change the value of the internal variables of the parent function outside the parent function. So, if you use the parent function as an object, take the closure as its common method, and take the internal variable as its private property (private value), be careful not to arbitrarily change the value of the internal variable of the parent function.

Understanding of JavaScript closure function

Related Article

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.