开发者

How to access class instance in event hanlder (JavaScript)?

开发者 https://www.devze.com 2022-12-14 10:38 出处:网络
Code: function Customer(name){ this._name=name; }; Customer.prototype.init=function(){ $(\'#SetCreditLevel\').cli开发者_开发问答ck(function(){

Code:

function Customer(name){
   this._name=name;
};

Customer.prototype.init=function(){
   $('#SetCreditLevel').cli开发者_开发问答ck(function(){
      //best way to access this._name ?
      //this now points to DOM element

   });
}


Something like this? You could override the value of this by setting your own context, but it is very useful to be able to access the DOM object as this in jQuery, and a fundamental part of how jQuery works. If you were to change that, I'd say you're not hardly using jQuery at all. So instead, i'm passing context as a parameter here...

function Customer(name){
   this._name=name;
};

Customer.prototype.init=function(){
   $('#SetCreditLevel').click((function(context){
       return function() {
           alert(context._name);
           alert(this);
       }
   })(this));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消