1. 程式人生 > >node.js初步了解(3)——慕課網(回調,作用域,上下文)

node.js初步了解(3)——慕課網(回調,作用域,上下文)

span clas global ava 運行 time log timeout color

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();//1 32 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();//0 48 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 globalV
12 
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 globalV
18         console.log(localV);//This is local var
19         console.log(innerLV);//This is inner local var
20     }
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 //2
15 function pet(words){
16     this.words=words;
17     console.log(this.words);
18     console.log(this);//指向的是global
19     console.log(this===global);//true
20 }
21 pet(‘...‘);
22 
23 //3
24 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 Wang
15 
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)——慕課網(回調,作用域,上下文)