setTimeout()의 this

5185 단어 settimeout
일:
 1 window.id='windowid';

 2 function M(){

 3     this.id='Mid';

 4     this.f1=function(){console.log(this.id);}; 

 5     this.f2=function(){

 6         var that=this;

 7         setTimeout(that.f1,5000);

 8         };

 9 }  

10 var m=new M(); 

11 m.f2();        //'windowid'    

이:
window.id='windowid';

function M(){

    this.id='Mid';

    this.f1=function(){console.log(this.id);}; 

    this.f2=function(){

        setTimeout(this.f1,5000);

        };

}  

var m=new M(); 

m.f2();        //'windowid'

삼:
window.id='windowid';

function M(){

    this.id='Mid';

    this.f1=function(){console.log(this.id);}; 

    this.f2=function(){

        setTimeout(function(){this.f1();},5000);

        };

}  

var m=new M(); 

m.f2();        //Error: undefined is not a function 

사:
 1 window.id='windowid';

 2 function M(){

 3     this.id='Mid';

 4     this.f1=function(){console.log(this.id);}; 

 5     this.f2=function(){

 6         var that=this;

 7         setTimeout(function(){that.f1();},5000);

 8         };

 9 }  

10 var m=new M(); 

11 m.f2();        //'Mid'

좋은 웹페이지 즐겨찾기