Jquery-extend Source Code Analysis

Source: Internet
Author: User

PS: This analysis is based on more than 2.0 versions;

jquery extend everyone is not unfamiliar, is also an important interface of jquery, wrote jquery components have been used extend it!

First of all, two $.extend () and $.fn.extend ();

When writing only one object argument, it is the form of the extension in jquery; $.extend ()->$.ajax;

The $.fn.extend is the extended jquery instance method; $.fn.extend->$ (). method;

Next look at the extend source code, first defined in jquery a number of variables:

var src, copyisarray, copy, name, options, clone,
target = Arguments[0] | | {},
i = 1,
Length = Arguments.length,
Deep = false; Whether it is a deep copy

Set the target as the first parameter, under normal circumstances the target element is an object, of course, the latter will do the work of jquery;

if (typeof target = = = "Boolean") {
Deep = target;
target = Arguments[1] | | {};
Skip the Boolean and the target
i = 2;
}

In the first judgment, the target object is judged whether it is a Boolean value, if so, the description is a deep copy, and the target element is set to the second parameter;

if (typeof target!== "Object" &&!jquery.isfunction (target)) {
target = {};
}

This time has determined that the target object is the object, and now that the target object is not an object or function, the target object is converted to object;

if (length = = = i) {
target = this;
I.;
}

The next judgment is to determine whether the plug-in form, if only write an object, the object is extended to the jquery source code, just to determine if the length and I are equal. If the work is equal, set target to This,this may be two cases, $ or $.prototype.

for (; i < length; i++) {//Case of multiple objects
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;
}
}
}
}

This for loop is a case of dealing with objects, and if there are multiple objects, multiple objects are extended to the first object. The IF in the For loop is as follows

if (options = arguments[i] = = null) {
Extend the Base Object
for (name in options) {
src = target[name];
copy = options[name];

First, determine whether the parameter has a value and then assign the various values. Here's the next:

if (target = = = copy) {
Continue
}

This if is judged to prevent circular references, such as:

$.extend (X,{name:x}), if you do not do the above if judgment, if written like this will cause a circular reference.

The next step is to determine whether a deep copy or a shallow copy

F (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;
}
}
}

First through deep && copy && (copy) jquery.isplainobject | | (Copyisarray = Jquery.isarray (copy))) To determine whether a deep-and deep copy copy--is an object or an array

Not meet the above conditions is a shallow copy.

if (Copyisarray) {
Copyisarray = false;
clone = src && jquery.isarray (src)? SRC: [];

} else {
clone = src && jquery.isplainobject (src)? src: {};
}

This judgment is a different processing of the different cases of copying arrays and JSON.

If SRC only writes src={}/src=[]; do not write src && jquery.isplainobject (src)? SRC: {}/src: [];

If the object A and object B have properties of the same name when multiple objects are inherited, the original attribute of a is overwritten, and the previous attribute is not overwritten by the processing of jquery.

target[name] = Jquery.extend (deep, clone, copy);

In the deep copy is actually the use of recursion, for different situations, respectively, processing.

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.