为什么jQuery.prototype中的init函数?我把它放在jQuery的闭包中,并且工作正常。我这样做:

(function( window, undefined ) {

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


谢谢,

埃里克·J。

最佳答案

我们不知道(向图书馆维护者咨询设计问题)。

但是将其作为公共属性使用确实允许overwriting or amending the constructor而不会损害jQuery函数,并且在需要将父构造函数应用于子实例的情况下,它使prototypical inheritance成为可能:

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);

关于jquery - 为什么init函数在jQuery.prototype中而不在jQuery的闭包中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18782973/

10-12 12:51
查看更多