node.js初步瞭解(3)——慕課網(回調,範圍,上下文)

來源:互聯網
上載者:User

標籤:運行   asc   llb   調用   rds   結果   解決   glob   callback   

1.

 1 //回調:回調是非同步編程最基本的方法,node.js需要按順序執行非同步邏輯的時候,一般採用後續傳遞的方式,將後續邏輯封裝在回呼函數中,作為起始函數的參數。 2 //具名函數 3 function learn(something){ 4     console.log(something); 5 } 6  7 function we(callback,something){ 8     something+=‘  is cool‘; 9     callback(something);10 11 }12 13 we(learn,"Nodejs");14 15 //匿名函數16 we(function(something){17     console.log(something)18 },‘linshuling‘)19 20 21 //順序執行,兩個按先後順序執行,可以得到我們想要的結果。22 var c=0;23 function printIt(){24     console.log(c);25 }26 function plus(){27     c+=1;28 }29 30 plus();31 printIt();//132 33 34 //由於plus函數順延強制,結果出現差異,可以使用回呼函數解決這一問題。35 var c=0;36 function printIt(){37     console.log(c);38 }39 function plus(){40     setTimeout(function(){41         c+=1;42     },1000);43     44 }45 46 plus();47 printIt();//048 49 50 //回呼函數51 var c=0;52 function printIt(){53     console.log(c);54 }55 function plus(callback){56     setTimeout(function(){57         c+=1;58         callback();59     },1000);60     61 }62 63 plus(printIt);//1

2.

 1 //變數範圍:和調用函數,訪問變數的能力有關。 2 var globalV="This is global var"; 3 function globalF(){ 4     var localV="This is local var"; 5  6     console.log(‘Visis global/local var‘); 7     console.log(globalV);//This is global var 8     console.log(localV);//This is local var 9 10     globalV="This is changed globalV";11     console.log(globalV);//This is changed globalV12 13     function localF(){14         var innerLV="This is inner local var";15 16         console.log("Visis globalV/localV/innerLV var");17         console.log(globalV);//This is changed globalV18         console.log(localV);//This is local var19         console.log(innerLV);//This is inner local var20     }21     localF();22 23 }24 globalF();

3.

 1 //上下文:在JavaScript中,this通常是指當前函數的擁有者,把擁有者叫做執行內容。this是JavaScript的一個關鍵字,代表函數運行時自動產生的一個內部對象。 2 //1 3 var pet={ 4     words:‘...‘, 5     speak:function(){ 6         console.log(this.words);//... 7         console.log(this===pet);//true 8  9     }10 }11 pet.speak();12 13 14 //215 function pet(words){16     this.words=words;17     console.log(this.words);18     console.log(this);//指向的是global19     console.log(this===global);//true20 }21 pet(‘...‘);22 23 //324 function Pet(words){25     this.words=words;26     this.speak=function(){27         console.log(this.words);28         console.log(this);29     }30 }31 32 var cat=new Pet(‘Miao‘);33 cat.speak();

4.

 1 //call 和 apply 2 var pet={ 3     words:‘...‘, 4     speak: function(say){ 5         console.log(say+‘ ‘+this.words); 6     } 7 } 8 // pet.speak("Speak"); 9 10 var dog={11     words:"Wang"12 }13 pet.speak.call(dog,‘Speak‘);//通過call改變執行內容,把pet.speak的this指向dog,後面是向該方法傳遞的參數。14 //Speak Wang15 16 17 18 19 20 function Pet(words){21     this.words=words;22     this.speak=function(say){23         console.log(this.words);24     }25 }26 function Dog(words){27     Pet.call(this,words);28     //Pet.apply(this,arguments)29 }30 31 var dog=new Dog(‘Wang‘);32 33 dog.speak();//Wang

 

node.js初步瞭解(3)——慕課網(回調,範圍,上下文)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.