博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Node_初步了解(3)回调,作用域,上下文
阅读量:6933 次
发布时间:2019-06-27

本文共 2964 字,大约阅读时间需要 9 分钟。

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

 

转载于:https://www.cnblogs.com/LinSL/p/7170447.html

你可能感兴趣的文章
在window平台下模拟Liunx使用GCC环境进行编译C的SO库。
查看>>
原来一直纠结MQ的用法,今天看到了一个最经典的例子。
查看>>
Resource is out of sync with the file system的解决办法
查看>>
交叉编译openssl不修改Makefile的方法
查看>>
linux 常用流量查看命令
查看>>
VMware ESXi Windows虚拟机磁盘扩展小结
查看>>
Linux常用命令
查看>>
方便的将数字转成字符串类型并在前面补0
查看>>
mysql主从复制
查看>>
宫崎骏首次因为自己的新作流泪
查看>>
SAP sybase16 安装的一些细节问题
查看>>
linux服务器同步时间
查看>>
Android开发常见问题及解决方法
查看>>
Linux 基础 - 磁盘管理 - 01
查看>>
我的友情链接
查看>>
繁忙的IT基础设施可能导致安全灾难
查看>>
Objective-C之Block
查看>>
iOS 图片加载框架-SDWebImage 解读
查看>>
安景业安京业安敬业anjingye
查看>>
java rest的说明
查看>>