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

前端 未结 1 1184
逝去的感伤
逝去的感伤 2020-12-06 15:02

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 jQu         


        
相关标签:
1条回答
  • 2020-12-06 16:00

    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);
    
    0 讨论(0)
提交回复
热议问题