fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[jQuery] jQuery.fn的init方法返回的this指的是什么对象?为什么要返回this?

Open haizhilin2013 opened this issue 6 years ago • 2 comments

[jQuery] jQuery.fn的init方法返回的this指的是什么对象?为什么要返回this?

haizhilin2013 avatar Sep 22 '19 12:09 haizhilin2013

init()方法中返回的this指向init的实例对象,init.prototype=jQuery.prototype,是jQuery的实例对象。 返回this实现链式调用。

watewww avatar Aug 04 '22 07:08 watewww

✂【回答】

  • jQuery.fn.init() 方法中的 this 指向的是一个类数组对象,而不是指向的 jQuery.fn
  • this 指向 init 的实例对象,即 $.fn.init.prototype,等于 jQuery.prototype,也是 jQuery 的实例对象。
  • 返回 this 是为了实现链式调用。

🖊【便签】

console.log($.fn.init() === jQuery.prototype);	// true

♡【关注】 https://blog.csdn.net/Hewes

Hub-ShengQing avatar Aug 05 '22 02:08 Hub-ShengQing