Javascript 帮助理解jQuery的jQuery.fn.init 为什么是fn中的init
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4754560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help understanding jQuery's jQuery.fn.init Why is init in fn
提问by Moshe K.
I was looking over the jQuery to better understand how it works. The constructor basically just calls
我正在查看 jQuery 以更好地了解它是如何工作的。构造函数基本上只是调用
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?
我想知道在 jQuery 的原型中使用 init 有什么意义?将其定义init()
为 jQuery 对象本身的一部分不会起到相同的作用吗?
Basically I would like to know why jQuery's init function is located at jQuery.fn.init()
and not jQuery.init()
基本上我想知道为什么 jQuery 的 init 函数位于jQuery.fn.init()
而不是jQuery.init()
Are there people doing this:
有没有人这样做:
jQuery('a').eq(0).hide().init('div').slideToggle(); //?
回答by mVChr
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.
编辑:重新阅读后,我认为这不能回答你的问题,但它可能有助于更好地理解 jQuery 的工作原理,所以我要离开它。
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()
!
正在发生的事情jQuery()
是被定义为jQuery.fn.init()
which 是另一种方式来说明jQuery.prototype.init()
哪个是选择器功能!这意味着没有人会打电话jQuery.fn.init()
或jQuery.init()
因为jQuery()
是.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:
在评论中,它只说了我所说的,但更简短。但这只是 jQuery 的本地副本……但是,如果您转到自执行函数末尾的第 908 行(版本 1.4.4),您将看到:
// 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!
...这意味着这个 localjQuery
变成了 global jQuery
。所以?所以...这个地方jQuery
实际上是jQuery.fn.init()
正确的?那么什么是init()
?如果您从第 100 行到 208 行查看,您会看到它是选择器方法。选择器方法是什么?这是你用所有的时间来寻找标签,标识,类,方法... $('#id')
,jQuery('.class')
,$('ul li a')
...选择器功能!
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.
所以没有人会打电话,jQuery.init('div')
因为它是jQuery('div')
那个任务之后的详细版本。请记住,jQuery.fn
这与所说的完全相同,jQuery.prototype
因此实际上该部分所做的只是将对象.init()
的原型分配为方法jQuery
。IE 一个 jQuery 插件。
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.
呼,那是一口。我希望这是有道理的,如果有人有任何更正,以防万一我在这个冗长的解释的任何部分误导了我,请告诉我。
回答by orca
$()
is an instanceof (new $())
is an instanceof (new $.fn.init())
$()
是一个实例(new $())
是一个实例(new $.fn.init())
The technique employed by jQuery is how you can achieve this. $()
always returns as if it was called with the new
keyword. However, instead of using a conditional switch on the this
reference inside function jQuery() {...}
it uses an external delegate object in all cases. This external delegate object, jQuery.fn.init() {...}
, is given the jQuery prototype so that its object 'type' is of jQuery
, and that all instances of it are actually instances of jQuery
.
jQuery 采用的技术是您如何实现这一点。$()
总是像用new
关键字调用一样返回。但是,它在所有情况下都使用外部委托对象,而不是在this
内部引用上使用条件开关function jQuery() {...}
。这个外部委托对象jQuery.fn.init() {...}
被赋予了 jQuery 原型,因此它的对象“类型”是 of jQuery
,并且它的所有实例实际上都是 的实例jQuery
。