javascript-anonymous function, closure application (3).

Source: Internet
Author: User

Mimic block-level scopes

1 functionbox ()2         {3             //A private scope can be implemented by the anonymous function that is contained in my execution4(function()                        5             {6                  for(varI=0 i<5; i++) 7                 {                            8 alert (i);9                 }   Ten                             //The variables used in this One})();//out of Here, destroyed. A              - alert (i); -         } the          -Box ();

This is the global private scope.

1 (function()2        {3             var age =100; 4             alert (age); 5         })(); 6         alert (age);

Private variables

1 //first, private variables are not accessible.2         functionbox ()3         {4             varAge = 100;//private variable, external unreachable5         }6 alert (age);7         8         //but the properties and methods are public.9         functionBox ()Ten         { One              This. age=100;//Properties, Public A              This. run=function()//method, the public -             { -                 return' Running ... ';  the             } -         } -       -         varbox1=NewBox (); + alert (box1.age); -Alert (Box1.run ());

★ Private variable Private function how to access through a public method [is actually the use of the role of the relationship (lexical scope) within the nested method can access variables, methods are common features to implement]

1 functionBox ()2         {3             //Private Variables4             varage=100;5             varnum= ' Jack ';6             7             //Private Functions8             functionRun ()9             {Ten                 returnnum+ ' Running ... ';  One             } A              -              This. Publicgo =function()//externally visible public interfaces, privileged methods -             { the                 returnAge+run (); -             } -              This. getage=function() -             { +                 returnAge ; -             } +         } A          at         varbox=NewBox (); -Alert (Box.publicgo ());//100Jack in operation ...

★ Pass parameters by constructor function

1 functionBox (value)2         {3             varUser=value;4              This. GetUser =function()5             {6                 returnuser;7             }8              This. setuser=function(value1)9             {TenUser=value1; One             } A         } -          -         varbox =NewBox (' Lee '); the alert (Box.getuser ()); -Box.setuser (' OOO ');//can set -Alert (Box.getuser ());

----But the problem is that anonymous functions are created multiple times and not shared resolved as follows

★ Block-level scope access static private variable private function

1(function()2         {3             varuser = ';//Private Variables4box=function(value)//Box does not have var so it is global, constructor5             {6user =value;7             };8Box.prototype.getuser=function()9             {Ten                 returnuser; One             } ABox.prototype.setuser=function(value) -             { -user =value; the             } -              -         })(); -      +         varbox=NewBox (' LEE '); -Alert (Box.getuser ());//LEE +          A         varBox2=NewBox (' KKK '); atAlert (Box.getuser ());//KKK -          -Box2.getuser (' oooo '); -Alert (Box.getuser ());//oooo -          -         //PS: Using prototype causes the method to be shared, and the user becomes a static property. (So-called static properties, which are properties shared in different objects)

The module mode-Simple to mention

1 //what is a single case, is always only instantiated once, in fact, is the literal object declaration of the way2 3         varbox = {//The first instantiation, cannot be instantiated the second time, then is a singleton4User= ' Lee ',5Runfunction()6             {7                 return' Travel in ... ';8             }9}
1 //privatization variables and functions2 varbox=function()3         {4             varuser = ' Lee ';//Private Variables5             functionRun ()//Private Functions6             {7                 return' In operation ';8             }9             Ten             return{//This is the method of foreign privilege . OnePUBLICGO:function()  A                 { -                     returnUser +run (); -                 } the             }; -             /* - var obj={//This is the method of foreign privilege - publicgo:function () +                 { - return user +run (); +                 } A             } at return obj; -             */     -         }(); -          -Alert (Box.publicgo ());

javascript-anonymous function, closure application (3).

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.