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 如果传经来的第一个参数为boolean , 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 = {}; } // 如果只传如一个就把target设为jquery对象 // extend jQuery itself if only one argument is passed--- if ( length === i ) { target = this; --i; //重置i=0 }
for ( ; i < length; i++ ) { // Only deal with non-null/undefined values 不处理值为传经来null和undefined 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 = false; clone = src && jQuery.isArray(src) ? src : [];