[英]jQuery structure - clarification?
I was reading this article about how jQuery works , and that article minified jquery structure into symbolic code: 我正在阅读这篇关于jQuery如何工作的文章,并且该文章将jquery结构缩小为符号代码:
/*1*/ var jQuery = (function ()
/*2*/ {
/*3*/
/*4*/
/*5*/ var jQuery = function (selector, context)
/*6*/ {
/*7*/
/*8*/
/*9*/ return new jQuery.fn.init(selector, context, rootjQuery);
/*10*/ },
/*11*/ rootjQuery;
/*12*/
/*13*/
/*14*/ jQuery.fn = jQuery.prototype = {
/*15*/ constructor: jQuery,
/*16*/ init: function (selector, context, rootjQuery)
/*17*/ {
/*18*/
/*19*/ if (!selector)
/*20*/ {
/*21*/ return this;
/*22*/ }
/*23*/ },
/*24*/ //I screwed with the core!
/*25*/
/*26*/ yo: function ()
/*27*/ {
/*28*/ alert("yo")
/*29*/ },
/*30*/ };
/*31*/
/*32*/ jQuery.fn.init.prototype = jQuery.fn;
/*33*/
/*34*/ // Expose jQuery to the global object
/*35*/ return jQuery;
/*36*/ })();
Line #32
is where the magic happens. #32
是魔术发生的地方。 so when I'm writing jQuery(..)
it actually run new init()
which has access to all jQuery.fn
functions. 因此,当我编写
jQuery(..)
它实际上运行了new init()
,它可以访问所有jQuery.fn
函数。
It's pretty clear (most of it) but I have 2 questions : 它很清楚(大部分)但我有两个问题:
Question #1 Why does line #15
( constructor: jQuery,
) exists ? 问题#1为什么
#15
行( constructor: jQuery,
)存在? all it does (imho) is to tell the prototype
object that it's ctor function
is jQuery
. 所有它(imho)是告诉
prototype
对象它的ctor function
是jQuery
。 how does jQuery use that fact ? jQuery如何使用这个事实?
Question #2 Looking at line #14
, it's obviously adding functions to jQUery.fn
( function yo
in our example - line #26
). 问题#2看
#14
行,它显然是在jQUery.fn
添加函数(在我们的例子中是函数yo
- #26
行)。
But why does jQuery.prototype
(line #14 middle) also have these functions (it sets them to it's prototype
...)? 但是为什么
jQuery.prototype
(第14行中间版) 也有这些函数(它将它们设置为它的prototype
......)? It's like we're going to do $.addClass()
which is invalid . 这就像我们要做的
$.addClass()
无效 。
Why does line #15 (
constructor: jQuery,
) exist?为什么第15行(
constructor: jQuery,
)存在? all it does (imho) is to tell the prototype object that it's ctor function is jQuery.所有它(imho)是告诉原型对象它的ctor函数是jQuery。
Yes. 是。 People do expect to find
new jQuery().constructor == jQuery
. 人们希望找到
new jQuery().constructor == jQuery
。
how does jQuery use that fact ?
jQuery如何使用这个事实?
For example, the pushStack
internal method uses this.constructor()
to create new instances - which also allows inheritance from jQuery . 例如,
pushStack
内部方法 使用this.constructor()
来创建新实例 - 这也允许从jQuery继承 。
But why does jQuery.prototype (line #14 middle) also have these [jQuery.fn] functions (it sets them to it's prototype...)?
但是为什么jQuery.prototype(第14行中间版)也有这些[jQuery.fn]函数(它将它们设置为它的原型......)?
To cite this answer : 举这个答案 :
One of the reasons behind this is certainly that they want to accomodate people that might modify
jQuery.prototype
instead ofjQuery.fn
.这背后的原因之一当然是他们希望容纳可能修改
jQuery.prototype
而不是jQuery.fn
。However another valid reason is that they perhaps wanted
somejQueryObj instanceof jQuery
to returns true, while it normally wouldn't.然而另一个有效的原因是他们可能希望
somejQueryObj instanceof jQuery
返回true,而通常不会。
It's like we're going to do
$.addClass()
which is invalid.这就像我们要做的
$.addClass()
无效。
No, how so? 不,怎么回事? Are you confusing the standard
.prototype
property of every (constructor) function with the internal prototype chain? 您是否将每个(构造函数)函数的标准
.prototype
属性与内部原型链混淆?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.