jQuery 1.7.1 代码研究 extend
生活随笔
收集整理的這篇文章主要介紹了
jQuery 1.7.1 代码研究 extend
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/* version : 1.7.1
* codeName : jQuery
* node author : Alfred lee
*/
// extend 函數是jQuery 的擴展函數。也是傳說中的深度拷貝
// 首行把 jQuery.extend jQuery.fn.extend 統一賦值一個函數。
jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
/* options 要擴展到源的某個對象 稱為選項
* name 索引名
* src 源的索引記錄
* copy 選項的索引記錄
* copyIsArray 是不是數組(用來判斷如何給原來的對象擴展參見下面的詳細分析)
* clone 復制體(源有的src的重新定義)
* target 源(被擴展的對象)
* i 記錄數(用來記錄從那里是要擴展到源的對象)
* length 參數長度
* deep 是否深度拷貝。即取值還是取址不知道和C的 * & 解釋類似
*/
// Handle a deep copy situation
// 是深度的時候
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// 如果不是深度或 擴展的源不是個對象。
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
//如果是對本身擴展。
// extend jQuery itself if only one argument is passed
if ( length === i ) {
target = this;
--i;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
// 對有效的內容開始擴展復制 這把逐個參數傳給了選擇項
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
//遍歷選擇項 把對應索引的源值和 選擇值保存對比。
src = target[ name ];//源
copy = options[ name ];//選擇項
//如果源就是要拷貝的(循環引用)防止死循環。
//不操作任何直接跳過繼續下一個
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// 如果是深度的時候 這時候判斷了是不是常規對象和要擴展進的是不是數組
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
//這里不知道為何重寫了copyIsArray 按道理應該每次都會重新判斷不用手動重寫的。
//這里是為了格式統一,數組對數組,對象對對象
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
//遞歸擴展
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
//不是深度就傳址
target[ name ] = copy;
}
}
}
}
// Return the modified object
// 返回的就是擴充后的了。也就是經常用來做默認值的方法。
return target;
};
* codeName : jQuery
* node author : Alfred lee
*/
// extend 函數是jQuery 的擴展函數。也是傳說中的深度拷貝
// 首行把 jQuery.extend jQuery.fn.extend 統一賦值一個函數。
jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
/* options 要擴展到源的某個對象 稱為選項
* name 索引名
* src 源的索引記錄
* copy 選項的索引記錄
* copyIsArray 是不是數組(用來判斷如何給原來的對象擴展參見下面的詳細分析)
* clone 復制體(源有的src的重新定義)
* target 源(被擴展的對象)
* i 記錄數(用來記錄從那里是要擴展到源的對象)
* length 參數長度
* deep 是否深度拷貝。即取值還是取址不知道和C的 * & 解釋類似
*/
// Handle a deep copy situation
// 是深度的時候
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// 如果不是深度或 擴展的源不是個對象。
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
//如果是對本身擴展。
// extend jQuery itself if only one argument is passed
if ( length === i ) {
target = this;
--i;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
// 對有效的內容開始擴展復制 這把逐個參數傳給了選擇項
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
//遍歷選擇項 把對應索引的源值和 選擇值保存對比。
src = target[ name ];//源
copy = options[ name ];//選擇項
//如果源就是要拷貝的(循環引用)防止死循環。
//不操作任何直接跳過繼續下一個
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// 如果是深度的時候 這時候判斷了是不是常規對象和要擴展進的是不是數組
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
//這里不知道為何重寫了copyIsArray 按道理應該每次都會重新判斷不用手動重寫的。
//這里是為了格式統一,數組對數組,對象對對象
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
//遞歸擴展
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
//不是深度就傳址
target[ name ] = copy;
}
}
}
}
// Return the modified object
// 返回的就是擴充后的了。也就是經常用來做默認值的方法。
return target;
};
轉載于:https://www.cnblogs.com/AlfredLee/archive/2012/02/01/jQuery_extend.html
總結
以上是生活随笔為你收集整理的jQuery 1.7.1 代码研究 extend的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样使用Eclipse来开发Androi
- 下一篇: asm字节码操作 方法的动态修改增加