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/

相关文章
相关标签/搜索