Simple Example of jQuery extend

Source: Internet
Author: User

Copy codeThe Code is as follows:
JQuery. extend = jQuery. fn. extend = function (){
Var options, name, src, copy, copyIsArray, clone,
Target = arguments [0] | | {},
I = 1,
Length = arguments. length,
Deep = false;

// Handle a deep copy situation
If (typeof target = "boolean "){
Deep = target;
Target = arguments [1] || {};
// Skip the boolean and the target
I = 2;
}

// Handle case when target is 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 (length = I ){
Target = this;
-- I;
}

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

// 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.