ericj
ericj

Reputation: 2301

Why is the init function in jQuery.prototype and not in jQuery's closure?

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

(function( window, undefined ) {

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

Thanks,

Eric J.

Upvotes: 2

Views: 894

Answers (1)

Bergi
Bergi

Reputation: 665040

We don't know (ask the library maintainers for their design desicions).

But having it available as a public property does allow overwriting or amending the constructor without harming the jQuery function, and it makes prototypical inheritance possible where you might need to apply the parent constructor on 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);

Upvotes: 3

Related Questions