javascript - "this" in my class inheritance -
i'm using john resig's class inheritance (for javascript) script. allows define "classes" can inherit from. i've found 1 limitation: cannot reference class structure/base class (this) within nested functions.
for example
var myclass = anotherclass.extend({ dostuff: function() { $('#myelem').animate({ top: 100 }, function() { this.saystuff('done'); }); }, saystuff: function(message) { alert(message); } }); var foo = new myclass(); foo.dostuff();
when script calls callback in turns calls this.saystuff
tries call method unnamed function i'm wrapping callback in, can't call chain of functions.
the way i've found around call variable created object, that's not idea since variable name should able change @ time.
any appreciated.
it out of scope.
dostuff: function() { var that=this; $('#myelem').animate({ top: 100 }, function() { that.saystuff('done'); }); },
and looks using jquery has proxy
dostuff: function() { $('#myelem').animate({ top: 100 }, $.proxy(this, this.saystuff, 'done'); }); },
Comments
Post a Comment