[英]Access an instance of a class from an event listener of one of the class components in javascript
[英]How to access class instance in event hanlder (JavaScript)?
碼:
function Customer(name){
this._name=name;
};
Customer.prototype.init=function(){
$('#SetCreditLevel').click(function(){
//best way to access this._name ?
//this now points to DOM element
});
}
類方法無法訪問屬性可能有助於我思考
像這樣的東西? 您可以覆蓋的值this
通過設置自己的背景,但它是能夠訪問DOM對象是很有用的this
在jQuery和jQuery的運行一個基本組成部分。 如果你要改變它,我會說你根本不習慣使用jQuery。 所以相反,我在這里傳遞上下文作為參數......
function Customer(name){
this._name=name;
};
Customer.prototype.init=function(){
$('#SetCreditLevel').click((function(context){
return function() {
alert(context._name);
alert(this);
}
})(this));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.