我reading this article about how jQuery works,这条精缩jQuery的结构为象征代码:#32
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*/ })();
行是魔法发生。所以当我写jQuery(..)
它实际上运行new init()
有权访问所有jQuery.fn
函数。
这是很清楚的(大部分),但我有2个问题:
问题#1为什么存在线#15
(constructor: jQuery,
)?它所做的(imho)是告诉prototype
对象它的ctor function
是jQuery
。 jQuery如何使用这个事实?
问题2纵观线#14
,这显然(在我们的例子功能yo
- 线#26
)添加功能jQUery.fn
。
但是为什么jQuery.prototype
(线#14中)也具有这些功能(它设置他们是prototype
...)?这就像我们要做$.addClass()
这是无效。
[差异的价值,原型和属性]可能的重复(http://stackoverflow.com/questions/12143590/difference-of-the-value-prototype-and-property) – Bergi
@Bergi您的答案不回答我的问题#1。你只是描述了这个事实。而已。关于我的问题#2,我不会在那里写下你的措辞:“对于那些不使用fn财产的人” - 你能否详细说明一下? –
好吧,我会带更具体的答案 – Bergi