In layman's text-closures (finishing) for beginners

Source: Internet
Author: User
Tags variable scope

 

/************** Scope *****************/
First understand the closure, you must first understand the JS special variable scope.

The scope of a variable is nothing more than two kinds: global variables and local variables.

The special point of JS is that local variables can be read directly inside the function.
On the other hand, a local variable inside a function cannot be read naturally outside the function. 、

It is important to note that when declaring variables inside a function, you must use the var command. If not, it actually declares a global variable.

How to read a local variable from the outside is within the function, defining a function.

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!


JS Code

Function F1 () {

n=999;//Global Variables

function F2 () {
alert (n);
}

return F2;

}

var result=f1 ();

Result (); 999

/************* Closure Package **************/
1. Concept
Closures are functions that can read other functions ' internal variables.


Since in JS, only sub-functions inside the function can read local variables, the closure can be simply understood 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.

2. Use
The maximum use of closures is two, and one is the ability to read variables inside the function. The other is to keep the values of these variables always in memory.

3. Note Points for using closures
Because the closure will make the function value of the variables are stored in memory, memory consumption will be very large, so can not abuse closures, otherwise it will reduce the performance of the Web page, in IE may lead to memory leak problems. The workaround is to delete all the non-applicable local variables before exiting the function.

The closure will change the value of the inner variable of the parent function outside of the parent function, so if you use the parent function as object objects, the closure as its common method, and the internal variable as its private property, you cannot arbitrarily change the value of the inner variable of the parent function.

Application mechanism of/************* closure **************/
JS private properties and private methods (cannot be accessed externally) through the secure implementation of the protection variables
function Constructor (...) {
var = this;
var membername = value;
function MemberName (...) {...}
}
The above three points are the most basic application scenarios for closures, and many of the classic cases are derived from this.

/************* JS garbage Collection garbage collection mechanism **************/
In JS, if an object is no longer referenced, the object will be recycled by GC. If two objects are referenced by each other instead of being referenced by a third party, then two mutually referenced objects are recycled, because function A is referenced by B and B is referenced by a c outside of a, which is why function A is not recycled after execution.

/************* Summary **************/
When an intrinsic function is referenced outside the scope that defines it, the inner function's closure is created, and if the intrinsic function references a variable that is located outside the function, when the external function is called, the variables are not freed in memory because the closures require them.

Official explanation: Closures are an expression that has many variables and environments that bind these variables, usually a function, in fact, in general, all functions in JS are a closure, but generally speaking, nested functions produce a more powerful closure, That's what most of us call closures.

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

Searches for the active object itself first, if it exists, returns if there is no active object that will continue to search for function A, and then finds it until it is found.
If B has a prototype prototype object, it finds its own prototype object after it finds its own active object, and then continues to look for it, which is the variable lookup mechanism in JS.
Returns undefined if the entire scope chain cannot be found, that is, when an object is found.

In this article, we will refer to two important words, the definition and execution of functions, the scope of the functions mentioned in the text is determined when the function is defined, not when it is executed.
For example:

function f (x) {
var g = function () {return x;}
return g;
}
var h = f (1);
Alert (H ());
The variable h in this code points to the anonymous function (returned by G) in F.

Assuming that the scope of the function h is determined by the execution alert (h ()), then the scope chain of H is: H's active object->alert the active object->window object.
Assuming that the scope of the function h is defined at the time of definition, the anonymous function pointed to by H has been scoped at the time of definition. Then, at execution time, the scope chain of H is: H's active object->f the active object->window object.
If the first hypothesis is true, the output value is undefined; if the second hypothesis is true, the output value is 1.

The result of the operation proves that the 2nd hypothesis is correct, stating that the scope of the function is indeed determined when the function is defined.

In layman's text-closures (finishing) for beginners

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.