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'이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
set Time out에 this가 문제를 가리키고 있습니다.위의 테스트를 실행합니다.fn () 때.처음에는 "Hello"로 돌아갈 수 있습니다.두 번째 후에'undefined'로 돌아왔습니다. 콘솔을.log (this.info) 를 console로 변경합니다.log(this...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.