码迷,mamicode.com
首页 > 其他好文 > 详细

setTimeout()里的this

时间:2015-04-22 13:13:52      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

1:

 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‘    

2:

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‘

3:

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 

4:

 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‘

 

setTimeout()里的this

标签:

原文地址:http://www.cnblogs.com/yigeqi/p/4446965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!