JQuery source code questions

前端 未结 2 389
天命终不由人
天命终不由人 2021-01-22 08:54

I got two questions about following code snippet.

(1). What is the purpose of \"return new jQuery.fn.init( selector, context, rootjQuery );\"? Why does it return another

2条回答
  •  不思量自难忘°
    2021-01-22 09:22

    (2). Why prototype.constructor is re-defined as JQuery?

    I think the reason is to keep a constructor reference inside each jQuery object, to actually itself (it creates a circular reference). In fact, by overriding the jQuery.prototype object with this piece of code

    jQuery.fn = jQuery.prototype = { ... }
    

    you lose the "automatically created" constructor (which points to the function it has been created from, in this case jQuery.fn.init), so you need to explicitly set it.

    I've found this link very helpful to understand javascript prototype and contructor:

    http://joost.zeekat.nl/constructors-considered-mildly-confusing.html

提交回复
热议问题