|
// Merge the attributes of two or more objects into the first object. Most of jQuery's subsequent functions are extended through this function.
// Most functions extended by jQuery. fn. extend call functions of the same name extended by jQuery. extend.
// If two or more objects are input, the attributes of all objects will be added to the first target object.
// If only one object is input, the attributes of the object are added to the jQuery object.
// In this way, we can add a new method for the jQuery namespace. It can be used to compile jQuery plug-ins.
// If you do not want to change the input object, you can pass in an empty object: $. extend ({}, object1, object2 );
// By default, the merge operation is not iterative. Even if a target attribute is an object or attribute, it will be completely overwritten rather than merged.
// If the first parameter is true, the merge operation is performed iteratively.
// Attributes inherited from the object prototype will be copied
// Undefined value will not be copied
// For performance reasons, attributes of the built-in JavaScript type are not merged.
// JQuery. extend (target, [object1], [objectN])
// JQuery. extend ([deep], target, object1, [objectN])
JQuery. extend = jQuery. fn. extend =Function(){
VarOptions, name, src, copy, copyIsArray, clone,
Target = arguments [0] | | {},
I = 1,
Length = arguments. length,
Deep =False;
// Handle a deep copy situation
// If the first parameter is boolean, it may be a deep copy.
If(TypeofTarget = "boolean "){
Deep = target;
Target = arguments [1] || {};
// Skip the boolean and the target
// Skip boolean and target, starting from 3rd
I = 2;
}
// Handle case when target is a string or something (possible in deep copy)
// If target is neither an object nor a function, it is forcibly set to an empty object.
If(TypeofTarget! = "Object "&&! JQuery. isFunction (target )){
Target = {};
}
// Extend jQuery itself if only one argument is passed
// If only one parameter is input, it is considered to be an extension of jQuery.
If(Length = I ){
Target =This;
-- I;
}
For(; I <length; I ++ ){
// Only deal with non-null/undefined values
// Only process non-empty Parameters
If(Options = arguments [I])! =Null){
// Extend the base object
For(NameInOptions ){
Src = target [name];
Copy = options [name];
// Prevent never-ending loop
// Avoid circular references
If(Target = copy ){
Continue;
}
// Recurse if we're merging plain objects or arrays
// Recursive
If(Deep & copy & (jQuery. isPlainObject (copy) | (copyIsArray = jQuery. isArray (copy )))){
// If copy is an array
If(CopyIsArray ){
CopyIsArray =False;
// Clone the corrected value of src
Clone = src & jQuery. isArray (src )? Src: [];
// If the copy object is
}Else{
// Clone the corrected value of src
Clone = src & jQuery. isPlainObject (src )? Src :{};
}
// Never move original objects, clone them
// Call jQuery. extend recursively
Target [name] = jQuery. extend (deep, clone, copy );
// Don't bring in undefined values
// The null value cannot be copied.
}ElseIf(Copy! =Undefined){
Target [name] = copy;
}
}
}
}
// Return the modified object
// Return the modified object
ReturnTarget;
}; |