javascript - Get right context(this) in callback -


i try call function in callback , class context(this). when calling callback function, doesn't have context. this undefined. tried few things bind(self) didn't work out.

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     }      this.connect("somecall",this.buildxml) } 

you need bind() function context, invoke it.

callb.bind(self)(xhttp.response); 

instead of

callb(xhttp.response).bind(self); 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -