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

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -