I have problem which still bothers me on js oop - I'm sure I'm doing it bad, but I cant get how to do it right.
For example, I have this code
Auth.prototype.auth = function () {
var request = new XMLHttpRequest();
request.open('GET', this.getAuthServerURL() + '/token', true);
request.send();
request.onloadend = function () {
var response = JSON.parse(request.responseText);
console.log(response);
if(response.result == 'found') {
var token = response.token;
this.setToken(token);
this.isSigned = true;
} else {
console.log('Not logged yet.');
}
}
}
The problem is that I cant access to function setToken from context of "request.onloadend" function - its probably because I lost reference to "this".
Whats a solution of this problem? Can I somehow pass the "this" var to context of this function?
Thanks!