Simulation code of jquery implementation principle-4 important extension function extend

Source: Internet
Author: User

Jquery. FN. Extend provides an extension mechanism to extend an object through one or more sample objects. If no extended object is specified, it will be extended to itself.

Jquery. Extend can also be used through jquery. FN. Extend. It is widely used in jquery to expand a member of a target object. Extension members come from a series of reference objects.
In this way, if we need to extend the removedata member for jquery. FN, we can do this.CopyCodeThe Code is as follows: jquery. FN. Extend (
{
Removedata: function (key ){
Return this. Each (function (){
Jquery. removedata (this, key );
});
}
}
);

The source code of extend is as follows, because it is relatively simple, so there is not much streamlining. Copy code The Code is 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 // In the case of deep copy, the first parameter is of the boolean type, which indicates deep copy, and 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 neither an object nor a function
If (typeof target! = "Object "&&! Jquery. isfunction (target )){
Target = {};
}
// If there is only one parameter, It is the extension of itself.
If (length = I ){
Target = this;
-- I;
}
// Traverse all reference objects and extend them 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.