Js closure and js Closure

Source: Internet
Author: User

Js closure and js Closure
The closure stores reference of the activity object of the external function, so if the closure does not end, these activity objects will not be reclaimed by the memory, in some cases, we can use this variable to prolong the time spent in the memory. The disadvantage is that the memory occupies a large proportion and affects the performance. In addition, ie9 used reference notation to collect garbage, which may easily cause memory leakage between html and js objects. Therefore, use closures with caution. Closures are useful when you want private variables. jquery plug-ins are all closures.
JS closure concept

We should first explain the characteristics of the closure to better understand it.

Closure has two features:

1. As a reference to a function variable-when a function is returned, it is activated.
2. A closure is a stack that does not release resources when a function returns.

In fact, the above two points can be merged, that is, when the closure function returns, the internal variables of the function are activated, and the stack zone of the function is retained.

We are familiar with mainstream languages such as C and java. If the return function is executed inside the function, the function returns the result and deletes the region where the function is located in the memory. the lifecycle is stopped. this is also true for common js functions.
However, js functions with the closure feature are somewhat special.
For example:
Function (){
Var I = 0;
Function B (){
Alert (++ I );
}
Return B;
}
Var c = ();
C ();

This is a standard closure. In function a, function B is defined, and function a returns the value of function B.
Var c = ();
C ();
These two statements are very important.
When var c = a (); is executed, a must have passed the return statement. According to the function features of mainstream languages, the value of c is the return value of.
The second row of c () actually executes the B function. finally, no matter who executes the task, a window with a value of 0 will pop up. By now, all the lifecycles are ended theoretically.
However, if we execute one more row.
Var c = ();
C ();
C ();
0 is displayed for the first time, but 1 is displayed for the second execution.

That is to say, after c () for the first time, I in a is retained. Naturally, a is retained in the memory stack.

A is a return, but the and internal values still exist. This is the closure.

Okay. To sum up,
1. the outer layer of the closure is a function.
2. There are functions in the closure.
3. The closure will return the internal function.
4. The function returned by the closure cannot have return. (because this is the end)
5. After the closure is executed, the internal variables of the closure will exist, but the internal variables of the closure internal function will not exist.

Application scenarios of closures (HA, copying references)
1. Protect variable security in the function. Taking the initial example as an example, in function a, I can only access function B, but cannot access function B through other channels, thus protecting the security of I.
2. Maintain a variable in the memory. Still, for example, because of the closure, the I in function a is always in the memory, so each execution of c () will add 1 to the I self.

Based on the application scenarios of reference materials, we will naturally think of java or c ++ classes. Although JS does not have the concept of classes, it has similar execution results of classes.

There is also a controversial format:
(Function (a, B) (a, B );
If you have used jquery and observed its code, you may wonder how it is written. Someone on the Internet also calls this format a closure.


Javascript Closure

Hello!

Calling c () is to call a (). This is a problem.
A () is a closure, and the content in it is to return B ().

The method of calling this closure should be a (); so the question just mentioned is here.
Var c = a (); is to pass the reference of this closure to c.
Can we understand that c now has a member variable I and a Member Method B, and its return value points to this member function.
For the benefit of closures, local variables are accessed after the function returns.
That is to say, when c () is run, the variable I in it is destroyed, but it can be retained in member method B.

You can call c () twice and a () twice to view the difference.

Hope to help you!

Bytes -----------------------------------------------------------------------------------------------------

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.