javascript - JS Refrencing "this" when in callback -


this question has answer here:

part 1:

how 1 dynamically reference parent property within callback?

for example if had structure.

var someclass = {     htmlclass : 'afunclass',     bindevents: function(){        jquery(function(){           alert( this.htmlclass );       });    }); } 

currently return "undefined" refrencing jquery function, rather parent.

part 2:

what dynamic way of doing this? supports next following condition.

var someclass = {     htmlclass : 'afunclass',     bindevents: function(){       var self = this;        jquery(function(){           if( !jquery('body').hasclass( self.htmlclass ) ){               self = {};           }       });    }); } 

currently if - someclass remains same, , doesn't become empty object. assigning directly variable doesnt seem work.

use variables scoped "this" want remember:

var someclass = {     htmlclass : 'afunclass',     bindevents: function(){       var = this;       jquery(function(){           alert( that.htmlclass );       });    }); } 

standards naming of local variable include self, this, that etc. go (i tend use this in object oriented js, work c# "thisy" me) :)


Comments

Popular posts from this blog

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

delphi - Indy UDP Read Contents of Adata -

qt - How to embed QML toolbar and menubar into QMainWindow -