jquery - using this keyword with javascript prototype addEventListener -
i want use keyword in event listener function.
var myviewmodel = function (file) { this.status = ""; this.xm = new xmlhttprequest(); this.xm.addeventlistener("load", this.onload, false); }; myviewmodel.prototype.onload = function (e) { this.status = "ok"; }; i can not access myviewmodel object this keyword in onload prototype.
- this object window.
- e parameter xmlhttprequest
how can access this?
you can solve problem jquery.proxy
this specifying function scope.
var myviewmodel = function (file) { this.status = ""; this.xm = new xmlhttprequest(); this.xm.addeventlistener("load", $.proxy(this.onload, this), false); }; myviewmodel.prototype.onload = function (e) { this.status = "ok"; }; now can use keyword myviewmodel.
Comments
Post a Comment