帮助理解jQuery的jQuery.fn.init为什么是在fn中的init [英] Help understanding jQuery's jQuery.fn.init Why is init in fn

查看:106
本文介绍了帮助理解jQuery的jQuery.fn.init为什么是在fn中的init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  new jQuery.fn.init 

我想知道在jQuery的原型中有什么要点?不会定义 init()作为jQuery对象的一部分本身也有同样的用途?






基本上我想知道为什么jQuery的init函数位于 jQuery.fn.init()而不是 jQuery有没有人这样做:

 

jQuery的( 'a')的当量(0).hide()的init( 'DIV')的slideToggle()。; //?


解决方案

编辑:线程我不认为这会回答你的问题,但是对于某人更好地了解jQuery如何工作,所以我要离开它可能是有用的。






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



看看你正在谈论的代码片段:





  //定义jQuery的本地副本
var jQuery = function(selector,context){
// jQuery对象实际上只是初始化构造函数'增强'
返回新的jQuery .fn.init(selector,context);
},

在评论中,它只是我说的,但更简单的说。但是这只是jQuery的本地副本...但是,如果您在自动执行功能结束时进入第908行(版本1.4.4),您将看到:

  //将jQuery暴露给全局对象
return(window.jQuery = window。$ = jQuery);

})();

...这意味着这个本地的 jQuery 成为全局 jQuery 。所以?所以...这个本地的 jQuery 实际上是 jQuery.fn.init()对吧?那么什么是 init()?如果你从100行到208行,你会看到它是选择器的方法。什么是选择方法?这是你一直使用的方法来查找标签,ids,类... $('#id') jQuery('。class ') $('ul li a') ...选择器功能!



所以没有人会调用 jQuery.init('div'),因为它是一个详细版本的 jQuery('div')之后。并记住, jQuery.fn 与说 jQuery.prototype 完全相同,所以真的所有这一切都在分配 .init()作为 jQuery 对象的原型的方法。 I.E.一个jQuery插件。



唉,那是一口气。我希望这是有道理的,如果任何人有任何更正,如果我在这段漫长的解释的任何部分被误解,请告诉我。


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

new jQuery.fn.init

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?


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

Are there people doing this:

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

解决方案

EDIT: 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.


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()!

What?

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

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

})();

...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!

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.

这篇关于帮助理解jQuery的jQuery.fn.init为什么是在fn中的init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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