帮助理解 jQuery 的 jQuery.fn.init 为什么 init 在 fn

我正在查看 jQuery 以更好地了解它的工作原理.构造函数基本上只是调用

I was looking over the jQuery to better understand how it works. The constructor basically just calls

new jQuery.fn.init

我想知道在 jQuery 的原型中包含 init 有什么意义?将 init() 定义为 jQuery 对象本身的一部分难道不能达到同样的目的吗?

I was wondering what is the point of having the init inside jQuery's prototype? Wouldn't defining init() as part of the jQuery object itself serve the same purpose?

基本上我想知道为什么jQuery的init函数位于jQuery.fn.init()而不是jQuery.init()

Basically I would like to know why jQuery's init function is located at jQuery.fn.init() and not jQuery.init()

有人这样做吗:

jQuery('a').eq(0).hide().init('div').slideToggle(); //?

推荐答案

重读后,我认为这不能回答您的问题,但它可能有助于更好地理解无论如何,jQuery 是如何工作的,所以我要离开它.

Upon re-reading I don't think this answers your question, but it might be useful for someone's better understanding of how jQuery works anyway so I'm leaving it.

发生的事情是 jQuery() 被定义为 jQuery.fn.init(),这是 jQuery.prototype 的另一种说法.init() 这是选择器函数!这意味着没有人会调用 jQuery.fn.init()jQuery.init() 因为 jQuery().init()!

What is going on is that jQuery() is being defined as jQuery.fn.init() which is another way to say jQuery.prototype.init() which is the selector function! What this means is that no one would call jQuery.fn.init() or jQuery.init() because jQuery() IS .init()!

什么?

让我们看看你说的那段代码:

Let's look at the piece of code you're talking about:

// Define a local copy of jQuery
var jQuery = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context );
    },

在评论中,它说出了我所说的,但更简短.但这只是 jQuery 的本地副本...但是,如果您在自执行函数的末尾转到第 908 行(版本 1.4.4),您会看到:

In the comments it says just what I said, but more briefly. But this is just the local copy of jQuery... however, if you go to line 908 (of version 1.4.4) at the end of the self-executing function you'll see:

// Expose jQuery to the global object
return (window.jQuery = window.$ = jQuery);

})();

...这意味着这个本地 jQuery 变成了全局 jQuery.所以?所以...这个本地 jQuery 实际上是 jQuery.fn.init() 对吗?那么什么是init()?如果您查看第 100 到 208 行,您会发现它是选择器方法.什么是选择器方法?这就是您一直使用的方法来查找标签、ID、类... $('#id')jQuery('.class')$('ul li a')...选择器函数!

...which means that this local jQuery becomes the global jQuery. So? So... this local jQuery was actually jQuery.fn.init() right? So what is init()? If you look from lines 100 to 208 you'll see that it's the selector method. What's the selector method? It's that method you use all the time to find tags, ids, classes... $('#id'), jQuery('.class'), $('ul li a')... the selector function!

所以没有人会调用 jQuery.init('div'),因为它是在分配之后的 jQuery('div') 的详细版本.请记住 jQuery.fnjQuery.prototype 完全相同,所以实际上所有这部分所做的就是将 .init() 分配为jQuery 对象的原型方法.IE.一个 jQuery 插件.

So no one would ever call jQuery.init('div') because it's a verbose version of jQuery('div') after that assignment. And remember the jQuery.fn is exactly the same as saying jQuery.prototype so really all that part is doing is assigning .init() as a method of the prototype of the jQuery object. I.E. a jQuery plugin.

唷,那是一口.我希望这是有道理的,如果有人有任何更正,以防我在这个冗长的解释的任何部分误传,请告诉我.

Phew, that was a mouthful. I hope this makes sense, and if anyone has any corrections in case I misinformed in any part of this lengthy explanation please let me know.

相关文章