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] | |
{};
Skip the Boolean and the target i = 2; //Handle case when the 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 are 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 = 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; };
Extend, can be used to jq its own extensions, can also be used for object copying
How to use:
1, to the JQ extension method
2, shallow copy of the object
3. Deep copy of Object
PS: Copy parameters can have multiple, such as $.extend (A,b,c,d ...)
A copy of all the following to a
Code Analysis:
var options, name, SRC, copy, Copyisarray, clone,
Options:arguments parameters
Name: Enum K value
SRC: The a.a in the above example, when A.A does not exist, is undefined
Copy: B.b in the above example
Copyisarray: Used to determine whether an array
Clone: Copy of SRC
1, the expansion of their own
target = Arguments[0] | | {},
i = 1,
Length = Arguments.length,
Deep = false;
Target: The object to assign a value to
For its own expansion, length must be 1, and the initial set of I equal, then set I to 0, after the for loop once;
if (length = = i) {
target = this;
I.;
}
For loops go straight here
if (copy!== undefined) {
target[name] = copy;
}
2, Shallow copy
The first argument Boolean is set to false because execution code execution defaults to shallow copy: Deep=false, the first argument is omitted. The incoming parameter is at least 2, $.extend (A,b,c,d ...)
This is a decision to format invalid arguments
if (typeof target!== "Object" &&!jquery.isfunction (target)) {
target = {};
}
For loops, the process goes in the same way as copies of itself
3, deep copy
The first parameter is definitely an object that true,for the loop index to an assignment of the value of index 1, and a in the list above.
if (typeof target = = "Boolean") {
Deep = target;
target = Arguments[1] | | {};
Skip the Boolean and the target
i = 2;
}
for (name in options) {
src = target[name];
copy = options[name];
Prevent never-ending Loop
if (target = = copy) {//when the assigned object and the copied value are the same object, jump out
Continue
}
recurse if we ' re merging plain objects or arrays
Deep copy && copy has value && copy is {}/New Object object or array
if (deep && copy && jquery.isplainobject (copy) | | (Copyisarray = Jquery.isarray (copy)) ) {
if (Copyisarray) {
Copyisarray = false;
If it is an array, it is referenced, used to hold the original value, and the empty group is assigned the value []
clone = src && jquery.isarray (src)? SRC: [];
} else {
If it is a {} object, the object is referenced to hold the original value, and the value is null to assign {}
clone = src && jquery.isplainobject (src)? src: {};
}
Never move original objects, clone them
This step is critical, since the invocation, the recursive copy, succeeds in replicating, preventing a common reference to the same object
target[name] = Jquery.extend (deep, clone, copy);
Don ' t bring in undefined values
else if (copy!== undefined) {
target[name] = copy;
}
}
The judgment of arguments parameters is then performed separately, and this method is well worth learning.