jQuery.extend函數

1 擴展jQuery靜態方法css

$.extend({
    test:function(){alert('test函數');}
})

用法: $.test()jquery

2 合併多個對象api

以jQuery.extend(css1,css2)爲例,css1和css中有一些屬性,extend函數會把css2中有的而在css1中沒有的屬性加到css1中,若是css2中的某個屬性在css1中也有,則會用css2的屬性覆蓋css1的同名屬性,css1就是最後的整合對象。或者也能夠用用:

var newcss = jquery.extend(css1,css2) newcss就是合併的新對象函數

var newcss = jquery.extend({},css1,css2) newcss就是合併的新對象,並且沒有破壞css1的結構。code

//用法:jQuery.extend(obj1,obj2,obj3,...)對象

var css1 = {size:"10px",style:"oblique"}繼承

var css2 = {size:"12px",style:"oblique",weight:"bolder"}ci

$.jQuery.extend(css1,css2)get

//結果:css1中的size屬性被覆蓋,並且繼承了css2的weight屬性it

//css1 = {size: "12px" , style:"oblique" , weight:"bolder"}

3 深度嵌套對象

新的extend()容許更深度的合併鑲套對象。

//之前的 .extend()

jQuery.extend(

{ name: "John", location: { city:"Boston" } },

{ last: "Resig", location: { state: "MA"} }

);

//結果: // => { name: "John", last: "Resig", location: {state: "MA"} }

//新的更深刻的.extend()

jQuery.extend( true,

{ name: "John", location: { city: "Boston" } },

{ last: "Resig", location: {state: "MA"} }

);

//結果 //=>{ name: "John", last: "Resig", location: { city: "Boston", state: "MA" }}

4 API網址http://api.jquery.com/jQuery.extend/

相關文章
相關標籤/搜索