Why is the init function in jQuery.prototype rather than closing jQuery?

Why is the init function in jQuery.prototype? I put it in jQuery closure and it works fine. I have done this:

(function( window, undefined ) { var jQuery = function( selector, context ) { return new init( selector, context, rootjQuery ); } var init=function( selector, context, rootjQuery ) { ... } ... })(...) 

Thanks,

Eric J.

+2
source share
1 answer

We do not know (ask the organizers of the library about your design intentions).

But the presence of a public property in it allows you to overwrite or modify the constructor without harming the jQuery function, and makes > possible when you may need to apply the parent constructor to the child instance:

 function MyJQuery(selector, context) { this.init(selector, context, MyJQuery.root); // <== // or more explicit: // jQuery.fn.init.call(this, selector, …); … } MyJQuery.fn = MyJQuery.prototype = Object.create(jQuery.fn); MyJQuery.root = jQuery(document); 
+3
source

Source: https://habr.com/ru/post/957303/


All Articles