node. js Preliminary Understanding (3)--MU class network (callback, scope, context)

Source: Internet
Author: User
Tags variable scope

1.

1 //callback: Callback is the most basic method of asynchronous programming, node. JS needs to execute asynchronous logic in order, usually in the way of subsequent passing, the subsequent logic is encapsulated in the callback function, as the parameters of the starting function. 2 //named function3 functionLearn (something) {4 Console.log (something);5 }6 7 functionWe (callback,something) {8something+= ' is cool ';9 callback (something);Ten  One } A  -We (Learn, "Nodejs"); -  the //Anonymous Functions -Wefunction(something) { - Console.log (something) -}, ' linshuling ') +  -  + //sequential execution, two sequential executions, can get the results we want.  A varC=0; at functionPrintIt () { - Console.log (c); - } - functionPlus () { -C+=1; - } in  - Plus (); toPrintIt ();//1 +  -  the //because the plus function defers execution, the result is different and you can use a callback function to resolve the problem.  * varC=0; $ functionPrintIt () {Panax Notoginseng Console.log (c); - } the functionPlus () { +SetTimeout (function(){ AC+=1; the},1000); +      - } $  $ Plus (); -PrintIt ();//0 -  the  - //callback functionWuyi varC=0; the functionPrintIt () { - Console.log (c); Wu } - functionPlus (callback) { AboutSetTimeout (function(){ $C+=1; - callback (); -},1000); -      A } +  thePlus (printit);//1

2.

1 //variable scope: relates to the ability to invoke a function and access a variable. 2 varGlobalv= "This is global var";3 functionglobalf () {4     varLocalv= "This is local var";5 6Console.log (' Visis global/local var ');7Console.log (GLOBALV);//This is global var8Console.log (LOCALV);//This is local var9 TenGlobalv= "This is changed Globalv"; OneConsole.log (GLOBALV);//This is changed Globalv A  -     functionlocalf () { -         varInnerlv= "This is inner local var"; the  -Console.log ("Visis globalv/localv/innerlv var"); -Console.log (GLOBALV);//This is changed Globalv -Console.log (LOCALV);//This is local var +Console.log (INNERLV);//This is inner local var -     } + localf (); A  at } -Globalf ();

3.

1 //context: In JavaScript, this usually refers to the owner of the current function, and the owner is called the execution context. This is a keyword of javascript that represents an internal object that is automatically generated when the function is run. 2 //13 varpet={4Words: ' ... ',5Speakfunction(){6Console.log ( This. words);//...7Console.log ( This===pet);//true8 9     }Ten } One pet.speak (); A  -  - //2 the functionPet (words) { -      This. words=words; -Console.log ( This. words); -Console.log ( This);//the point is global +Console.log ( This===global);//true - } +Pet (' ... '); A  at //3 - functionPet (words) { -      This. words=words; -      This. speak=function(){ -Console.log ( This. words); -Console.log ( This); in     } - } to  + varcat=NewPet (' Miao '); -Cat.speak ();

4.

1 //Call and apply2 varpet={3Words: ' ... ',4Speakfunction(say) {5Console.log (say+ "+ This. words);6     }7 }8 //Pet.speak ("speak");9 Ten vardog={ OneWords: "Wang" A } -Pet.speak.call (dog, ' speak ');//change the execution context by call, point the Pet.speak this to the dog, followed by the arguments passed to the method.  - //Speak Wang the  -  -  -  +  - functionPet (words) { +      This. words=words; A      This. speak=function(say) { atConsole.log ( This. words); -     } - } - functionDog (words) { -Pet.call ( This, words); -     //pet.apply (this,arguments) in } -  to vardog=NewDog (' Wang '); +  -Dog.speak ();//Wang

node. js Preliminary Understanding (3)--MU class network (callback, scope, context)

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.