zoukankan      html  css  js  c++  java
  • Node_初步了解(3)回调,作用域,上下文

    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
  • 相关阅读:
    分答是什么?
    判定表
    总结
    周结
    第五周周结
    周结
    一周总结(18周)
    一周总结(17周)
    一周总结(16周)
    一周总结(15周)
  • 原文地址:https://www.cnblogs.com/LinSL/p/7170447.html
Copyright © 2011-2022 走看看