相关疑难解决方法(0)

var self = this?

使用实例方法作为事件处理程序的回调改变的范围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)

它有效,但这是最好的方法吗?这对我来说很奇怪.

javascript jquery closures scope

180
推荐指数
4
解决办法
10万
查看次数

标签 统计

closures ×1

javascript ×1

jquery ×1

scope ×1