App.Order = {
oForm : $('#order-form'),
expertMode : false,
expertModeData : null,
init : function() {
$('#toggle-expert-mode').on('click', function() {
this.switchMode();
});
}
}
如何绑定(bind)this
在点击事件处理程序中。这也是添加事件监听器的正确方法吗?我的意思是在 init 方法中?
正在做App.Order.switchMode()
没有意义,因为如果我更改对象名称会发生什么?
谢谢
最佳答案
How do i bind
this
inside the click event handler.
确实如你所说。您绑定(bind)(此)
:
App.Order = {
// ...
init : function() {
$('#toggle-expert-mode').on('click', function() {
this.switchMode();
}.bind(this));
}
}
关于javascript 对象 - 如何绑定(bind)到 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20332242/