jquery.extend function Source Explanation

Source: Internet
Author: User

Test Code 1:

var x = {name: "Codeplayer", age:20};
var y = {age:18};
var z = {site: "www.365mini.com", age:21  };
var obj = $.extend (x, y, z);
Under this call, Target is arguments[0] that is the X object
IsArray Function Source Analysis:

IsArray:Array.isArray | | function (obj) {
return jquery.type (obj) = = = = "Array";
},
Test Code 2:

$ (document). Ready (function () {
var x = {name: "Codeplayer", students:[1,2,3]};
var y = {name: "xxx", students:[3,4,5]};
Alert (Jquery.extend (true,x,y) students);//return 3,4,5, because the key name of two students is the same as the array subscript, so it will be overwritten!
Iteration x, Y will iterate over the students collection, and then Y's studenets is IsArray return true
//This time target is the X object,
});
Test Code 3:

var x = [n/a];
var y = [3,4,5];
Alert (Jquery.extend (true,x,y));//output 3,4,5
because of the above for: In the loop every iteration of the name is 0,1,2, which is the subscript of the array, so it will continue to overwrite the front! When an array property is present in an object, this is the case and the test code 2 is the same!
Test Code 4:

var x = {name: "Qinliang", age:12,length:2}
var y = {name: "xxx", Age:23,sex: "Female", length:3}
var result= Jquery.extend (true,x,y);
for (var name in result)
{
 alert (name+ ", +result[name]);//print sex
}
It is in the options and the second Y iteration, X is the target, so when the name,age,length is done, it falls down to the sex attribute of Y. Src=target[name],copy=options[name],name is sex, at this time copy is not undefined, but female, when the direct encapsulation to target above it can be.
Test Code 5:

var x = [n/a]
var y = [7,8,9,10]
var result=jquery.extend (true,x,y);
for (var name in result)
{
 alert (name+ "-+result[name]);//output 7.8.9.10 because the for-in iteration name is 0,1,2,3, The first three subscripts are overwritten, and the following subscript is added directly to target!
}
Test Code 6:

var x = { 
sayhi:function () {
   alert ("This is the new test method");
}
};
Only one parameter means omitting the target parameter, the target parameter defaults to the jquery object itself
var obj = $.extend (x);
Alert (obj = = $); True
$.sayhi ();//This is the new test method
//target is used as the base object to extend the properties and methods, if there is no first boolean parameter, then the first argument is the second argument
var x = { Name: "Codeplayer", age:20};
var y = {age:18};
var z = {site: "www.365mini.com", age:21  };
var obj = $.extend (x, y, z);
Alert (obj = = = x); True
});

Test Code 7:

If target is not an object and target is not a function, set Target to null object {}
alert ($.extend); The first one is either Boolean,target or 1, But 1 is not object and typeof 1 is not [object Object]
alert (typeof 1);
jquery Source Analysis:

Jquery.extend = JQuery.fn.extend = function () {var src, copyisarray, copy, name, options, clone,//get first parameter target = Argum Ents[0] | |
{}, I = 1,//Gets the number of arguments length = arguments.length,//default is shallow copy deep = false;
Handle a deep copy situation//If the first parameter is a Boolean type, then assign the parameter to the local variable deep if (typeof target = = = "Boolean") {deep = target;
Skip the Boolean and the target//target is the first parameter, if the first parameter is a Boolean, then save directly as the second parameter, just to match the call method one and call way two! target = arguments[I] | |
{};
i++; }//Handle case when Target was a string or something (possible in deep copy) if (typeof target!== "Object" &&! Jquery.isfunction (target)) {target = {};}//Extend JQuery itself if only one argument is passed//if there is a single parameter, then I is 1,len
Gth is 1, then set target to the caller, the jquery object itself! At the same time, I was reduced to 0//alert ($.extend (1)); if (i = = length) {target = this;//here is jquery, because it is called by jquery! i--;}//Loop pass in the parameters set for (; i < length; i++) {// Only deal with non-null/undefined values//Get parameter-by-argument if (options = arguments[i])! = NULL) {//ExTend the base object//To iterate over the properties of each object type parameter for (name in options) {src = target[name];//Get the options of the current iteration with the same Name property value, because Opti
The ONS is an object that has been iterated over and over, but target is going to return the object, so he is "static" copy = options[name];
Prevent Never-Ending loop//If the original attribute is the same as the one that is now iterated, as in the example above, X and Y,z have the age attribute! If the same then continue with the following loop, for example if age is equal to 18, continue to the next loop! if (target = = = copy) {continue;}//Recurse If we ' re merging plain objects or arrays//copy is the current iteration out of the attribute value, deep whether it represents a depth clone, if The current iteration is either the {} or the object constructed by the new object or the array type or the class array type///So it is to be deeply cloned if (Deep && copy && (Jquery.isplainobject ( copy) | | (Copyisarray = Jquery.isarray (copy))) )//If it is a class array object, [All-in-one] if (copyisarray) {Copyisarray = false;//If the original name property of target is an array, then clone is the original array//Call target[N
AME] = Jquery.extend (deep, clone, copy); In other words, the clone will continue to be called as Target, and the result of the recursive call is set to the students of the previous collection//So the subsequent copy will overwrite the preceding clone clone = src && jquery.isarray (SRC)?
SRC: []; } else {//not class Array object clone = src && jquery.isplainobject (src)? src: {};}//Never move original objects, Clone them//Let clone as Target, copy all the properties above clone to clone above! That is, clone it all to clone. target[name] = Jquery.extend (deep, clone, copy); 
Don ' t bring in undefined values} else if (copy!== undefined) {//If the deep is false, the above if is not satisfied, then it is a shallow-level clone, then the result is to overwrite the previous property in the back
Because this direct assignment results in overwriting, the same property is given to the target object target[name] = copy;
}}}//Return the modified object return target; };

Summarize:

(1) The first parameter indicates whether deep cloning, if the deep clone will be the array or object to split the attributes, and then traverse the property rather than directly overwrite!

(2) The default value of the deep parameter is False, you can explicitly specify a true value for the parameter, but you cannot explicitly specify a value of false. In short, the first argument cannot be a false value. If the argument is null or undefined, the parameter is ignored.

(3) If only one parameter is specified for $.extend (), it means that the parameter target is omitted. At this point, target is the jquery object itself. In this way, we can add new functions to the global object jquery. There is only one parameter that will satisfy the i===length, so target is this! At this time I is 0, which means to inherit the first parameter property with this one!

(4) The case of the bottom depth cloning is the above test code 2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.