Analysis of _javascript techniques in JavaScript closure cases

Source: Internet
Author: User
Tags closure
Official explanation
A "closure" is an environmental expression (usually a function) that has many variables and binds them, and therefore these variables are also part of the environment expression.
Popular Explanations
All the functions in JavaScript are a closed package. In general, however, nested function produces a more powerful closure, which is what we call a "closure" most of the time. Look at the following code:
Copy Code code as follows:

<script type= "Text/javascript" >
<!--
outer function A
function A () {
Temporary variable i
var i = 0;
Inner layer function B
Function B () {
Reference Outer TEMP Variable i
alert (++i);
}
Execute the result, return the inner layer function b
return b;
}
Execute the outer function A, and give C a reference to the inner-layer function B
If you follow normal understanding, I will be collected by GC after this statement is executed, at which point I shall be "undefine"
var C = A ();
To perform the inner function, because of the closure, function B I still refers to the external temporary variable I
C ();
-->
</script>

After the script has executed the Var c=a (), the variable C actually points to the variable i in the function b,b, then executes C () and a window pops up to show the value of I. This piece of code is actually a closure, why? Because the variable C outside function A refers to function B within function A.
JavaScript's garbage collection mechanism
Because JavaScript is a special garbage collection mechanism, it leads to the closure of the package. The broad rules of the JavaScript garbage collection mechanism are as follows:
In JavaScript, if an object is no longer referenced, the object is reclaimed by GC. If two objects are referenced to each other and are no longer referenced by the 3rd person, the two objects that are referenced are also reclaimed. In the script above, function A is referenced by B, and function B is referenced by C outside of function A, which is why function A is not reclaimed after execution.
Closure of the application scenario
1. Protect the variables within the function security. In the example above, the variable i in function A has only function B to access, and cannot be accessed by other means, thus protecting the security of I.
2. Maintain a variable in memory. Still as the example above, because of the closure, I in function a always exists in memory, so every time I execute C (), I will add 1.
3. Implement JS private property and private method (cannot be accessed externally) by protecting variable's security. as follows, private and private methods are inaccessible outside of constructor:
Copy Code code as follows:

Function Constructor (...) {
var that = this;
var membername = value;
function MemberName (...) {...}
}
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.