为什么init函数在jQuery.prototype中而不在jQuery的闭包中? [英] Why is the init function in jQuery.prototype and not in jQuery's closure?

查看:67
本文介绍了为什么init函数在jQuery.prototype中而不在jQuery的闭包中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ) {
        ...
    }
    ...
})(...)

谢谢

Eric J.

推荐答案

我们不知道(向图书馆维护者询问设计意图).

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

但是将其作为公共财产使用确实允许覆盖或修改构造函数而不会造成损害jQuery函数,它使原型继承可能需要在其上应用父构造函数的地方子实例:

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

这篇关于为什么init函数在jQuery.prototype中而不在jQuery的闭包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆