2010-12-03 12 views
1

의 내가 클래스가 있다고 가정 해 봅시다 :자바 스크립트 객체의 기능 범위 지정

var asdf = new Class({ 
    myFunction: function() { 
    //some stuff here 
    }, 
    anotherFunction: function() { 
    globalObject.dosomethingandusecallback( 
     function() { // this is the callback 
     //how do I call myFunction() here? I can't seem to get it to work? 
     } 
    ); 
    } 
}); 

나는 내 콜백 함수의 정의에 myFunction이 전화를하려고 몇 가지 범위 지정 문제를 갖고있는 것 같다. 내가 여기서 무엇을 놓치고 있니? 이 컨텍스트에서 myFunction에 액세스해야한다고 생각했습니다.

감사합니다.

답변

3

복사 콜백 함수의 변수 외부에 this 키워드 및 콜백 내에서 그 변수를 사용

anotherFunction: function() { 
    var self = this; 
    globalObject.dosomethingandusecallback( 
    function() { // this is the callback 
     self.myFunction(); 
    } 
); 
}