The implementation principle of jquery simulation code-4 Important extension functions Extend_jquery

Source: Internet
Author: User
Tags extend
JQuery.fn.extend provides an extension mechanism that allows us to extend an object through one or more sample objects. If you do not specify an object to be extended, it is extended to yourself.

Jquery.extend can also be used by JQuery.fn.extend, which is used in jQuery to extend members to a target object, with extended members coming from a series of reference objects.
In this way, if we need to removedata the Jquery.fn extended member, we can do so.
Copy Code code as follows:

JQuery.fn.extend (
{
Removedata:function (key) {
Return This.each (function () {
Jquery.removedata (this, key);
});
}
}
);

Extend source code as follows, because it is simpler, so did not do too much streamlining.
Copy Code code 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//Deep copy case, the first argument is a Boolean type, then, a deep copy, 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 not an object or a function
if (typeof target!== "Object" &&!jquery.isfunction (target)) {
target = {};
}
If there is only one argument is to expand yourself
if (length = = i) {
target = this;
I.;
}
Iterate through all the reference objects and extend 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;
};

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.