在 jQuery 的 API 手冊中可以看到,extend 實際上是掛載在 jQuery 與 jQuery.fn 上的兩個不同方法。雖然 jQuery 內部用相同的代碼實現 jQuery.extend() 與 jQuery.fn.extend(),但它們的功能並不相同。先看官方 API 的說明:
jQuery.extend(): Merge the contents of two or more objects together into the first object.(把兩個或多個物件合併到第一個當中)
jQuery.fn.extend(): Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(把物件掛載到 jQuery 的 prototype 屬性,以擴充新的 jQuery 實例方法)
由於 jQuery 有靜態方法與實例方法之分,因此兩者的第一個差異就在於:一個用來擴充靜態方法,一個用來擴充實例方法。用法如下:
jQuery.extend({
sayhello: function (){
console.log( "Hello,This is jQuery Library" );
}
})
$.sayhello(); //Hello, This is jQuery Library
jQuery.fn.extend({
check: function () {
return this .each( function () {
this .checked = true ;
});,
uncheck: function () {
return this .each( function () {
this .checked = false ;
}); }
})
$( "input[type='checkbox']" ).check(); //所有的 checkbox 都會被選擇
要注意兩種呼叫方式:一種直接用 $ 呼叫,另一種用 $() 呼叫。此外 jQuery.extend() 可接收多個物件作為參數:只有一個參數時,會把該物件的屬性方法附加到 jQuery 上;有多個參數時,則把後面物件的屬性方法附加到第一個物件上。以下是 extend 的實現源碼:
jQuery.extend = jQuery.fn.extend = function () {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false ;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
i = 2;
}
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
if ( length === i ) {
target = this ;
--i;
}
for ( ; i < length; i++ ) {
if ( (options = arguments[ i ]) != null ) {
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
if ( target === copy ) {
continue ;
}
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false ;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
target[ name ] = jQuery.extend( deep, clone, copy );
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
return target;
};
乍看之下這一大段代碼頗難理解,其實大部分都在處理多參數時的物件合併與深拷貝問題。若把這些功能拿掉,只保留擴充靜態與實例方法的核心,代碼會精簡成:
jQuery.extend = jQuery.fn.extend = function (obj){
var target= this ;
for ( var name in obj){
copy=obj[name];
if (target === copy) continue ;
if ( typeof copy === 'undefined' ) continue ;
target[name]=copy;
}
return target;
}
下面再逐段為 extend 方法加上註解說明:
jQuery.extend = jQuery.fn.extend = function () {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false ;
// 對接下來的參數進行處理
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
i = 2;
}
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
if ( length === i ) {
target = this ;
--i;
}
// 對從 i 開始的多個參數進行遍歷
for ( ; i < length; i++ ) {
if ( (options = arguments[ i ]) != null ) {
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
if ( target === copy ) {
continue ;
}
// 遞歸處理深拷貝
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false ;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
target[ name ] = jQuery.extend( deep, clone, copy );
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
return target;
};
弄懂了 jQuery 擴充的原理,相信往後再撰寫 jQuery 插件時就不再那麼棘手了,希望這份解析對你的開發有所幫助。