1. 程式人生 > >jquery原始碼分析之jQuery物件初始化

jquery原始碼分析之jQuery物件初始化

在jquery實際應用中可通過$或jQuery生成jquery物件,如$("#hr_three")可生成jquery物件,jquery是如何做到的呢?

jquery載入時,入口為如下匿名方法,

(function( global, factory ) {...} ([color=red]typeof window !== "undefined" ? window : this, function( window, noGlobal) {...}[/color]));


紅色部分為呼叫引數,global為window物件,factory為匿名方法,factory指向的匿名方法包含了整個jquery的主要功能程式碼。

在jquery載入呼叫預設方法時,通過factory( global );對factory指向的匿名方法進行呼叫,可以看到匿名方法呼叫時第二個引數並未傳入,因此noGlobal引數為undefined,以下程式碼初始化了window全域性物件的jQuery及$屬性。

if ( !noGlobal ) {
// noGlobal為undefined,因此if判斷為真
window.jQuery = window.$ = jQuery;
}

將jQuery物件賦值給了全域性的jQuery及$屬性,因此,可以在jquery環境中使用jQuery或$來生成jquery物件。

再來看下jQuery物件的初始過程,

jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context );
}

init = jQuery.fn.init = function( selector, context, root ) {
...
// this實際上指向的是jQuery物件,根據selector的類別,對jQuery物件做了很多屬性增強封裝並最終返回
return this;
}

為什麼this會指向jQuery物件呢? 看以下程式碼

init.prototype = jQuery.fn;

jQuery.fn為jQuery的別名(參照下面程式碼),而init的prototype物件又指向jQuery.fn,因此init也是jQuery物件的別名,this就指向jQuery物件了。

jQuery.fn = jQuery.prototype = {
jquery: version,
constructor: jQuery,
...
}

以上程式碼jQuery.fn賦值為jQuery.prototype物件,並將其自身的構造方法指向 jQuery,因此jQuery.fn實際上就是jQuery物件的別名。為什麼要為jQuery設定別名呢?個人感覺是想將jQuery物件方法用別名區分開來。

以上即為jQuery物件的初始框架。

如下程式碼段模擬了jQuery物件生成的骨架程式碼:

var test = function() {
return new test.fn.init();
};

test.fn = test.prototype = {
constructor: test,

add: function() {
}
};

var init = test.fn.init = function() {
// 增加長度屬性
this.length = 1;
// 增加0屬性
this[ 0 ] = "test";
// 返回自身
return this;
}

init.prototype = test.fn;

var test = test();
console.log(test.length + "---" + test[0]);