Jquery. Extend function usage!

Source: Internet
Author: User

[Original]

1. Extend the jquery static method.

 

1 $. Extend ({
2 Test: Function () {Alert ( ' Test function ' )}
3 })

 

 

Usage: $. Test ()

2. Merge multiple objects.
For jquery. Extend (css1, css2), css1 and css2 have some attributes.
The extend function adds the attributes of css2 but not of css1 to css1. If a property of css2 and a property name of css1 are used, the attributes of css2 are used to overwrite the attributes of CSS 1 with the same name. Css1 is the final sum object. Or you can use:
VaR newcss = jquery. Extend (css1, css2) newcss is the new object to be merged.
VaR newcss = jquery. Extend ({}, css1, css2) newcss is the new object to be merged, and does not destroy the structure of css1.

 

Code

1 // Usage: jquery. Extend (obj1, obj2, obj3 ,..)
2 VaR Css1 = {Size: " 10px " , Style: " Oblique " }
3 VaR Css2 = {Size: " 12px " , Style: " Oblique " , Weight: " Bolder " }
4 $. Jquery. Extend (css1, css2)
5 // Result: The size attribute of css1 is overwritten and inherits the weight attribute of css2.
6 // Css1 = {size: "12px", style: "oblique", weight: "Bolder "}
7  

 

 

3. Deep set object
The new extend () allows you to more deeply combine inlaid objects. The example below is a good proof.

 

Code

1   // Previous. Extend ()
2 Jquery. Extend (
3 {Name: "John", Location: {City: "Boston "}},
4 {Last: "resig", Location: {state: "Ma "}}
5 );
6 // Result:
7 // ==>{ Name: "John", last: "resig", Location: {state: "Ma "}}
8 // New more in-depth. Extend ()
9 Jquery. Extend ( True ,
10 {Name: "John", Location: {City: "Boston "}},
11 {Last: "resig", Location: {state: "Ma "}}
12 );
13   // Result
14   // ==>{ Name: "John", last: "resig ",
15   // Location: {City: "Boston", state: "Ma "}}
16

 

 

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.