Javascript, jquery (closure concept)

Source: Internet
Author: User

Occasionally, when I hear about JavaScript closures, I think of the closures that I used to learn about compilation principles and digital logic. The closures I used to talk about in class are hard to understand and contain recursive meanings, now I don't want to look at the closure concept.

But I often use JavaScript, so I need to understand the concept here.

In fact, the closure concept in Javascript is very simple, that is, the function uses external variables and can be obtained without passing parameters.

For example:

 

<SCRIPT>

 

VaR smessage = "Hello World ";

Function sayhello (){
Alert (smessage );
}

Sayhello ();

Addnumber (1, 2 );

 

VaR ibasenum = 10;

Function addnumber (inum1, inum2 ){
Function doaddition (){
Alert (inum1 + inum2 + ibasenum );
}
Return doaddition ();
}

 

Function (){
VaR I = 0;
Function B (){
Alert (++ I );
}
Return B;
}

VaR c = ();
C ();
C ();

</SCRIPT>
The first function sayhello does not transmit parameters. It directly uses the smessage variable, which is called a closure.

The second function is complex. There is a doaddition which is also a closure function. It does not need parameters and gets inum1, inum2, and the external variable ibasenum in the execution environment.

The third function can protect the access to the I variable and keep saving I in the memory, which can be increased all the time. (A classic use of closures)

Closure in jquery is similar. Let's give an example first.

 

You may ask (function ($ ){
$ ("Div P"). Click (function () {alert ("cssrain! ")});
}) (Jquery); // a closure
What is the writing method?

Don't worry. I also consulted UPC to understand it a little.

$ Here is only a form parameter, but jquery is a global variable, so it will be automatically executed without calling this function, or in two steps

Is to convert to a normal function, first write the function, then call.

As shown below
Actually:
(Function ($ ){
$ ("Div P"). Click (...);
}) (Jquery );
Is equal
Function tempfunction ($) {// create a function with $ as the parameter
$ ("Div P"). Click (....);
}
Tempfunction (jquery); // input the real parameter jquery to execute the function.

Simply write it like this. Forget it.

 

(Function (cssrain ){
Cssrain ("Div P"). Click (....);
}) (Jquery); // a closure

 

Basic Syntax of closures:
(Function () {do someting })();
// You can define an anonymous function and execute it immediately.
With parameters:
(Function (form parameter) {do someting}) (real parameter );
In addition
(Function () {var UPC = "I am UPC "})();
Alert (UPC );
Undefined is displayed.
Because after the closure, the variables in it are equivalent to local.

Advantages of closures:
No additional global variables are added,
All variables are in the anonymous function during execution.

The above example is not very good. It is a bit confusing with the closure of JavaScript, but it is indeed a closure in jquery. It's just processed by jquery.

If something is wrong, we will discuss it with each other. I am also a beginner and there are many other things I don't know.

 

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.