Professional JavaScript For Web developers 2nd book notes 3rd

Source: Internet
Author: User

As mentioned above, JavaScript does not have the syntax block similar to C # and Java. Variables defined in the for loop can still be used after the loop body is removed, for example:

Function Outputnumbers (count ){
For ( VaR I = 0 ; I < Count; I ++ ){
Alert (I );
}

Alert (I ); // Count
}

In addition, even if a variable is declared, and the variable is declared after the value is assigned, there will be no problem. js will ignore repeated declarations.

 

Function Outputnumbers (count ){
For ( VaR I = 0 ; I < Count; I ++ ){
Alert (I );
}

VaR I; // Variable redeclared
Alert (I ); // Count
}

In C # and Java, the syntax block is used to control the scope of variables. js cannot be used directly like this and a work und is required, for example:

( Function (){
// Block Code here
})();

The declaration of a function is surrounded by a pair of parentheses. Therefore, JS considers that parentheses are expressions, and variables within the expression range cannot be referenced externally, therefore, we can simulate the concept of similar blocks, such:

 

Function Outputnumbers (count)
{
( Function ()
{
For ( VaR I = 0 ; I < Count; I ++ )
{
Alert (I );
}
})();

Alert (I ); // Causes an error
}

The variables defined in the anonymous function are destroyed when the execution of the anonymous function ends. Therefore, they cannot be referenced outside, but the anonymous function can access count because the anonymous function forms a closure.

 

Benefits of using this method:

1. this method can effectively reduce the objects defined in the Global Context window. For pages developed by multiple users, it can reduce the occurrence of name conflicts, every developer can use the names they like without worrying about the global context window.

 

( Function (){

VaR Now =   New Date ();
If (Now. getmonth () =   0 & & Now. getdate () =   1 ){
Alert ("Happy" New Year ! ");
}

})();

2. This method effectively reduces the memory issue of the closure, because it does not point to the reference of the anonymous function, so the anonymous function scope chain will be destroyed after the function is executed.

 

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.