Help understanding jQuery's jQuery.fn.init Why is init in fn

后端 未结 2 1587
离开以前
离开以前 2020-12-04 11:21

I was looking over the jQuery to better understand how it works. The constructor basically just calls

new jQuery.fn.init

I was wondering w

2条回答
  •  星月不相逢
    2020-12-04 12:07

    $() is an instanceof (new $()) is an instanceof (new $.fn.init())

    The technique employed by jQuery is how you can achieve this. $() always returns as if it was called with the new keyword. However, instead of using a conditional switch on the this reference inside function jQuery() {...} it uses an external delegate object in all cases. This external delegate object, jQuery.fn.init() {...}, is given the jQuery prototype so that its object 'type' is of jQuery, and that all instances of it are actually instances of jQuery.

提交回复
热议问题