使用实例方法作为事件处理程序的回调改变的范围this从"我的实例"到"无论只是调用的回调".所以我的代码看起来像这样
function MyObject() {
this.doSomething = function() {
...
}
var self = this
$('#foobar').bind('click', function(){
self.doSomethng()
// this.doSomething() would not work here
})
}
Run Code Online (Sandbox Code Playgroud)
它有效,但这是最好的方法吗?这对我来说很奇怪.