A brief analysis of JS closure package

Source: Internet
Author: User
Tags one more line

Believe that most programmers who have been exposed to JS programming are more or less familiar with the closure of JS, the so-called "closure", refers to a lot of variables and the environment that binds these variables (usually a function), so these variables are also part of the expression. Closures are one of the most powerful features of ECMAScript (JavaScript), but the premise of good closures is the need to understand closures. Closures are relatively easy to create, and people may even inadvertently create closures, but these unintentionally created closures are potentially harmful, especially in a more common browser environment. If you want to use closures as a feature of your weaknesses, you must understand how they work. The implementation of the closure mechanism is largely dependent on the role of the scope in the parsing of identifiers (or object properties).

Let me give you a simple function to illustrate how closures work:

The main language we know, like C,java, is that as soon as the return is executed inside the function, the function returns the result, and then the area of the function is deleted in memory. The life cycle stops. The usual JS function is the same.
but the JS function with closure features is a bit special.
For example:
function A () {
var i=0;
function B () {
alert (++i);
return B;
var C = a ();
c ();

This is a standard closure. The function b,a is defined in function A and return the value of B. These can be a matter of first.
var C = a ();
c ();
the implementation of these two sentences is very important.
in var C = a (); In this line, a function is executed, then a is sure to pass the return. According to the function characteristics of the main language, the value of C is now the return value of a .
the second line of C () is actually performed by the B function. Finally, regardless of who is executing, a window with a value of 0 pops up, so that all life cycles end in theory.
However, if we do one more line.
var C = a ();
c ();
c ();
The first pop-up was 0, and the second execution popped 1.

That is, after the first C (), I in a is still retained. Natural a remains in the memory stack area.

A is return, but a and internal values still exist, which is the closure.
In a general program, we use closures mainly for the use of a variable, in the scope of a function defined variables, will not affect the subsequent variables, but this method can be called the value of the variable, the main function is to protect the security of the function.
The above is a simple JS function closure of the examples.

A brief analysis of JS 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.