Whenever I attempt to invoke a function within a callback and utilize the class context(this), I face an issue where the callback function doesn't carry any context. The value of this
ends up being undefined. I have experimented with using bind(self)
, but unfortunately, it did not yield the desired outcome.
export class AppComponent {
connect(call, callb) {
var self = this;
var xhttp = new XMLHttpRequest();
xhttp.responseType = responseType;
xhttp.open("GET", "http://localhost:3000/" + call, true);
xhttp.onreadystatechange = function(){
if (xhttp.readyState == 4 && xhttp.status == 200)
{
callb(xhttp.response).bind(self);
}
};
xhttp.send(null);
}
buildXML(response){
console.log(this) //prints undefined, should print AppComponent or something
}
this.connect("someCall", this.buildXML);
}