Jquery.extend () method
- You can merge objects
- Deep copy and shallow copy
SOURCE Analysis:
Overview: 1. Define variables First
options: Save arguments[i for Each loop traversal],
name: Save the key value of the loop traversal object
src: Save the target object's properties
copy: Saving properties of a merged object
Copyisarray: If copy is an array, save with Copyisarray
Clone: If the target object is an array, save with clone.
target: Destination Object
Deep: A Boolean value that determines whether a dark copy
2. Then determine if it is a deep copy, and if the value becomes target,i to 2, skip the first two parameters.
3. Determine if target is an object or function, and if not, turn target into an empty object
4. Determine if there is only one parameter in the case
5. Traverse the parameters that need to be extended target to determine if deep is true, and if so, use recursion to start a dark clone, if not shallow
6. If it is a shallow clone, simply assign the copy value to Target[name].
Note: The first parameter is not supported to write false, if it is a shallow copy do not write.
1 //the Extend method is the prototype extension method for jquery objects and init objects2 //It also has the function of extending ordinary objects independently.3Jquery.extend = JQuery.fn.extend =function() {4 /*5 *target objects that are extended6 number of *length parameters7 *deep whether deep operation8 */9 varoptions,Ten name, One SRC, A Copy, - Copyisarray, - Clone, thetarget = Arguments[0] | | {}, -i = 1, -Length =Arguments.length, -Deep =false; + //Target is the first parameter, and if the first argument is a Boolean value, the target is assigned to the deep - //depth Indicates whether deep-face replication is performed, and when True, deep replication is performed, otherwise only the first layer is extended + //then assign the second parameter to target . A if(typeoftarget = = = ' Boolean ') { atDeep =Target; -target = Arguments[1] | | {}; - //assign I to 2, skip the first two parameters -i = 2; - } - //target is neither an object nor a function to set target to an empty object. in if(typeofTarget!== ' object ' &&!jquery.isfunction (target)) { -target = {}; to } + //If there is only one argument, the jquery object is assigned to target, which extends to the jquery object - //Extend (True, {}); the //extend (obj); * if(length = = =i) { $ //This ==> Jquery/jquery.fnPanax Notoginsengtarget = This; - //if i=2, I is to let the loop start from 1, if i=1, I is to let the loop start from 0 the--i; + } A //start traversing parameters that need to be extended to target the for(; i < length; i++) { + //handles the I-extended object, which removes objects other than deep and target - if(options = Arguments[i])! =NULL) { $ //Traverse all the traversed properties of the I-Object $ for(Nameinchoptions) { - //Save the properties of the target object with SRC -src =Target[name]; the //to save the properties of a merged object with copy -copy =Options[name];Wuyi //If two attributes are equal, you do not need to merge the the if(target = = =copy) { - Continue; Wu } - //recursive merge When the user wants a deep operation About //copy is a pure object or an array $ if ( -Deep && -Copy && -(Jquery.isplainobject (copy) | | (Copyisarray =jquery.isarray (copy ))) A ) { + //if it is an array the if(Copyisarray) { - //Reset the Copyisarray to false to prepare for the next traversal. $Copyisarray =false; the //determine if SRC is an array in the extended object theclone = src && jquery.isarray (src)?src: []; the}Else { the //determine if SRC is not a pure object in the object being extended -clone = src && jquery.isplainobject (src)?src: {}; in } the //recursively call the Extend method to continue the deep traversal theTarget[name] =jquery.extend (deep, clone, copy); About } the //If deep copy is not required, direct copy (the value of the key that is traversed in the I-extended object) the Else if(Copy!==undefined) { theTarget[name] =copy; + } - } the }Bayi } the //the original object is changed, so if you do not want to change the original object, target can pass in {} the returnTarget; -};
Example:
If you want to merge the above two objects
It takes 6 steps.
Jquery.extend () Source Code Analysis