Supplemental $.extend ()

Source: Internet
Author: User
Tags jquery cdn

Thanks for the reminder of a child's shoes! Say my last essay. jquery plug-in development of the way one can also be used for merging parameters and deep clone, although the way to do a method of a combination of parameters, but not described in detail, so today do a bit of a supplement here!

first, merging parameters

The Extend extension prototype for jquery is:

Extend (arg,arg1,arg2,......);

return value not arg1,arg2 ... Merge to Arg. There are two ways to use it.

    • Omit the arg parameter, then arg1,arg2 ... Returns after merging, such as:

var results=$.extend ({name: ' Nick '},{sex: ' Handsome '});//Then the return value is results={name: ' Nick ', Sex: ' Handsome '}, custom arg parameter, The arg of the custom type is returned. For example://defines ARG as an object var results=$.extend ({},{name: ' Nick '},{sex: ' Handsome '});//Then, the return value is arg object results={name: ' Nick ', sex: The ' handsome '};//here defines arg as an array var results=$.extend ([],{name: ' Nick '},{sex: ' Handsome '});//Then, the return value is the arg array results=[name: ' Nick ', Sex: ' Handsome '];

Attention:
When merging parameters, if the attribute name of the parameter in $.extend () conflicts, the following value replaces the previous value. Such as:

This defines arg as an object var results=$.extend ({},{name: ' Nick ', Sex: ' Male '},{sex: ' Handsome '});//Then, the return value is the Arg object. Sex attribute conflicts, the following value handsome replace Maleresults={name: ' Nick ', Sex: ' Handsome '};
Second, clone

The Extend clone of jquery is:

$.extend (Boolean,arg,arg1,arg2 ...);
    • When Boolean is true, deep clone:

var results=$.extend (true,{},{user:{name: ' Nick ', Love: ' Money '},age:25},{user:{name: ' Summer ', like: ' Car '},sex: ' Handsome '});//return value results={"user": {"name": "Summer", "Love": "Money", "like": "Car"}, "age": "Sex": "Handsome"};
    •   When the Boolean is False (default):

var results=$.extend (false,{},{user:{name: ' Nick ', Love: ' Money '},age:25},{user:{name: ' Summer ', like: ' Car '},sex: ' Handsome '});//return value results={"user": {"name": "Summer", "like": "Car"}, "age": "Sex": "Handsome"};

Have you noticed, dear? Take a good look at the return value!
Attention:

    • It is best to set the Boolean to True when $.extend () is used for clone.
    • Since earlier versions of jquery had only shallow clones, but later versions were fixed, use the jquery CDN as much as possible with the higher version.

Supplemental $.extend ()

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.