내에서 자바 스크립트 객체 메소드를 호출하는 MyClass
및 사용자 스크립트의 방법 다음내가 정의 콜백
function MyClass() {
this.myCallback = function() {
alert("MyClass.myCallback()");
};
this.startRequest = function() {
GM_xmlhttpRequest({
'method': 'GET',
'url': "http://www.google.com/",
'onload': function (xhr) {
myClassInstance.myCallback();
}
});
};
}
var myClassInstance = new MyClass();
myClassInstance.startRequest();
이 스크립트가 작동하고 myCallback()
방법은 GM_xmlhttpRequest 완료되면 호출되는합니다.
그러나이 코드는 onload
콜백이 전역 변수 myClassInstance
을 참조하기 때문에 작동합니다. 나는에 onload
콜백 업데이트하는 경우 :
Uncaught TypeError: Object [object DOMWindow] has no method 'myCallback'.
그것은 this
잘못된 맥락에서 평가되고있는 것 같다 :
'onload': function (xhr) {
this.myCallback();
}
를 다음 나는 (크롬) 오류가 발생합니다.
메서드를 myClassInstance
으로 호출하는 방법은 전역 변수를 사용하지 않고도 가능합니까?
+1 나를 때려. – JAAulde
+1 방금 세션을 끝내고 긴 시간을 보냈습니다. 고맙습니다. :) – Anthony