2

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.

4

1 に答える 1

3

わかりません (ライブラリの保守担当者に設計上の決定について尋ねてください)。

ただし、パブリック プロパティとして使用できるようにすると、関数を損なうことなくコンストラクターを上書きまたは修正できます。また、子インスタンスに親コンストラクターを適用する必要がある場合に、プロトタイプの継承が可能になります。jQuery

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);
于 2013-11-03T22:43:29.950 に答える