Talking about javascript--closure package

Source: Internet
Author: User

The concept of closures

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".

Scope of the variable

To understand closures, you must first understand the special variable scope of JavaScript.

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

The special point of the JavaScript language is that the global variables can be read directly inside the function.

var n=999;function f1(){  alert(n);}f1(); // 999

A local variable inside a function cannot be read naturally outside the function.

function f1(){  var n=999;}alert(n); // error

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

function f1(){  n=999;}f1();alert(n); // 999
How do I read 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(){ n=999; function f2(){   alert(n); // 999 }}

In the above code, the function F2 is included inside the function F1, and all local variables inside the F1 are visible to the F2.

But the opposite is not possible, F2 internal variables, the F1 is not visible. This is the JavaScript-specific "chain-scoped" structure (chain scope), where child objects look up the variables of all the parent objects one level at a level. Therefore, all the variables of the parent object are visible to the child object, and vice versa.

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(){  n=999; function f2(){   alert(n); } return f2;}var result=f1();result(); // 999
Use of closures
    • Can read variables inside the function
    • Keep the values of these variables always in memory
Note Points for closures
    • Because closures will always keep variables in the function in memory, memory consumption is large, can not be abused, otherwise it will cause performance problems. Memory leaks can be caused in IE. The workaround is to remove all unused local variables before exiting the function.
    • 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.
The garbage collection mechanism of JavaScript

In JavaScript, if an object is no longer referenced, the object is recycled by the GC. If two objects are referenced by each other and are no longer referenced by the 3rd, then the two mutually referenced objects are also recycled. Because function A is referenced by B and B is referenced by a c outside of a, this is why function A is not recycled after execution.

The road long its repair far XI, I will go up and down and quest ~ ~

Talking about javascript--closure package

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.