In a user script, I have defined the MyClass
and its methods as follows:
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();
This implementation successfully triggers the myCallback()
method after the completion of the GM_xmlhttpRequest.
However, it is worth noting that the success relies on the direct reference to the global variable myClassInstance
within the onload
callback. If the callback were updated to:
'onload': function (xhr) {
this.myCallback();
}
An error would occur in Chrome:
Uncaught TypeError: Object [object DOMWindow] has no method 'myCallback'.
It appears that the evaluation of this
is incorrect in this context.
Is there an alternative approach to invoking the myCallback()
method of myClassInstance
without relying on a global variable?