如何工作 jQuery 就绪

How work jQuery ready

本文关键字:jQuery 就绪 工作 何工作      更新时间:2023-09-26

我有对象:

function myObj (){
  this.name = "noName";
}
myObj.prototype = {
  init: function(){
    console.log(this);
    this.setName();
  },
  setName: function(){
    this.name = "object name";
  }
}

var obj = new myObj();

在我打电话之后:

jQuery(obj.init)

控制台.log(这个)初始化#document

但是如果我从chrome控制台称其为手动

obj.init();

它给了我我的Obj { ... }

有人可以解释我的WTF吗?

这里没有什么具体的ready。这完全是关于this如何运作的。

jQuery(obj.init)传递obj.init的值,所以当函数被调用时,它不会在obj的上下文中被调用,所以this是不同的。

如果要创建提供特定上下文的包装函数,请使用 bind

jQuery(obj.init.bind(obj));