Introduction to javascript closures (situ zhengmei) _ javascript tips-js tutorial

Source: Internet
Author: User
Today, we can see the use of closures without any worries, and sort out the closure items. The definition of a closure is very obscure-a closure refers to a syntax domain located in a specific area with a continuous reference (read/write) section of the non-persistent variable value capability in the execution domain outside the region itself. The non-persistent variables of these external execution domains magically keep their values (deep links) when they are initially defined (or created) in the closure ). To put it simply, the closure stores a copy of the variable (key-Value Pair) it acquires from the upper-level function or scope in another scope ), however, these key-value pairs will not be destroyed as the upper-level functions are executed. Zhou aimin makes it clearer that a closure is an "Attribute Table", a closure is a data block, and a closure is a comparison table containing "Name = Value. That's simple. However, it must be emphasized that closures are a concept of runtime.
Closure in Javascript has two features:
As a reference to a function variable-when a function is returned, it is activated.
A closure is a stack that does not release resources when a function returns.
There are three types of closure implementations that are currently quite accepted:

The Code is as follows:


With (obj ){
// Here is the object Closure
}


The Code is as follows:


(Function (){
// Function Closure
})()


The Code is as follows:


Try {
//...
} Catch (e ){
// Catch closure, but not in IE
}


Several useful examples

The Code is as follows:


// ************* Closure uniqueID *************
UniqueID = (function () {// Save the value of the called object of this function
Var id = 0; // This is the private permanent value
// The outer function returns a nested function with the right to access the permanent value.
// That is the nested function stored in the variable uniqueID.
Return function () {return id ++;}; // return, auto-increment.
}) (); // Call the outer function after definition.
Document. writeln (uniqueID (); // 0
Document. writeln (uniqueID (); // 1
Document. writeln (uniqueID (); // 2
Document. writeln (uniqueID (); // 3
Document. writeln (uniqueID (); // 4


The Code is as follows:


// ************* Closure factorial *************
Var a = (function (n ){
If (n <1) {alert ("invalid arguments"); return 0 ;}
If (n = 1) {return 1 ;}
Else {return n * arguments. callee (n-1 );}
}) (4 );
Document. writeln ();


The Code is as follows:


Function User (properties ){
// A variable must be declared to point to the current instance.
Var objthis = this;
For (var I in properties ){
(Function (){
// In the closure, t is new every time, and the value of properties [I] is in
Var t = properties [I];
Objthis ["get" + I] = function () {return t ;};
Objthis ["set" + I] = function (val) {t = val ;};
})();
}
}
// Test code
Var user = new User ({
Name: "Bob ",
Age: 44
});
Alert (user. getname ());
Alert (user. getage ());
User. setname ("Mike ");
Alert (user. getname ());
Alert (user. getage ());
User. setage (22 );
Alert (user. getname ());
Alert (user. getage ());


The following are the questions you can see today:
Requirements:
Make the Onclick events of the three nodes correctly pop up corresponding parameters.

The Code is as follows:



  • Aa

  • Aa

  • Aa


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.