Jquery. FN. Extend provides an extension mechanism to extend an object through one or more sample objects. If no extended object is specified, it will be extended to itself.
Jquery. Extend can also be used through jquery. FN. Extend. It is widely used in jquery to expand a member of a target object. Extension members come from a series of reference objects.
In this way, if we need to extend the removedata member for jquery. FN, we can do this.CopyCodeThe Code is as follows: jquery. FN. Extend (
{
Removedata: function (key ){
Return this. Each (function (){
Jquery. removedata (this, key );
});
}
}
);
The source code of extend is as follows, because it is relatively simple, so there is not much streamlining. Copy code The Code is as follows: // <Reference Path = "jQuery-core.js"/>
2
3
4 jquery. Extend = jquery. FN. Extend = function (){
5 // copy reference to target object
6 var target = arguments [0] | |{}, I = 1, length = arguments. length, deep = false, options, name, SRC, copy;
7
8 // In the case of deep copy, the first parameter is of the boolean type, which indicates deep copy, and the second parameter is the target object.
9 If (typeof target = "Boolean "){
Deep = target;
Target = arguments [1] || {};
// Skip the Boolean and the target
I = 2;
}
// If the target is neither an object nor a function
If (typeof target! = "Object "&&! Jquery. isfunction (target )){
Target = {};
}
// If there is only one parameter, It is the extension of itself.
If (length = I ){
Target = this;
-- I;
}
// Traverse all reference objects and extend them to the target object
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 object literal values or Arrays
If (Deep & Copy & (jquery. isplainobject (copy) | jquery. isarray (copy ))){
VaR clone = SRC & (jquery. isplainobject (SRC) | jquery. isarray (SRC ))? SRC
: Jquery. isarray (copy )? []: {};
// 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;
};