Home > Article > Web Front-end > Share the detailed usage of jQuery.extend function
Recently studying jQuery. Record the usage of jQuery.extend extensionfunction.
1. Extend jQuerystatic method.
1##$.extend(
2test:function()'test function')}
3})Usage: $.test()
objects .## is jQuery.extend(css1, css2) as an example. css1 and css2 have some attributes
(The method will still deal with it, here we talk about attributes).The extend function will add the attributes that css2 has but does not have in css2 to css1. If a certain attribute of css2 shares the name of a certain attribute of css1, then The properties of css2 will be used to overwrite the properties of the same name of css1. css1 is the final integration object. Or you can also use: var newcss = jquery.extend(css1,css2) newcss is the merged new object. var newcss = jquery.extend({},css1,css2) newcss is the merged new object. And it does not destroy the structure of css1.
1 //Usage: jQuery.extend(obj1,obj2,obj3,..)
2var 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 Css26
// Css1 = {size: "12px",style: "oblique",weight: " bolder"}7
##3. Deeply nested objects
The new
extend
() allows you to merge nested objects more deeply. The example below is a good proof.
1 // Previous .extend()
## 2 jQuery. extend(
3 ##{ name: “John”, location: }, 4
##{ last: “Resig”, location: { state: “MA ” } ## 5
); 6
// Result: 7
// => { name: “John”, last: “Resig”, location: { state: “MA” } } 8
// New and more in-depth .extend() 9 jQuery.extend( true,
10 { name: “John”, location: { city: “Boston” } },
11 { last: “Resig”, location: { state: “MA” } }
12 );
13 // 结果
14 // => { name: “John”, last: “Resig”,
15 // location: { city: “Boston”, state: “MA” } }
16
17
The above is the detailed content of Share the detailed usage of jQuery.extend function. For more information, please follow other related articles on the PHP Chinese website!