javascript - the callback function doesn't see 'this' -


my callback function doesn't see 'this'

here code of binding keypress:

     initialize: function () {         $(document).bind('keyup', this.keypress);         _.bindall(this, 'rotate');      },      rotate: function (event) {         //smth...      } 

here function keypress:

     keypress: function(event) {         console.log(event.keycode);         var pagex = 0, pagey = 0;         var ev = new object();         ev.pagey = pagey;         ev.pagex = pagex;          func = this.rotate;         func(ev);         //also tried this:         //this.rotate(ev);     } 

it error: "uncaught typeerror: undefined not function"

i can't it.

i appreciate help, thank you!

the problem here in jquery, bind adds event listener, not set context of 'this'

you need bind method context, using underscore.

e.g.

 initialize: function () {     $(document).bind('keyup', _.bind(this.keypress, this));     _.bindall(this, 'rotate');  },  rotate: function (event) {     //smth...  } 

that ensure 'this' refers correct object


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -