- 擴展方法
extend(dest,src1,src2,src3,...)
//將src1,src2,src3...等合并到dest中沧卢,返回值為合并后的dest
//如果不想改變的dest結構
var newSrc = $.extend({},src1,src2,src3...)
var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})
//結果
result = {name: "Jerry", age: 21,sex: "Boy"}
==后面參數與前面參數有相同的名字蚁堤,后面的會覆蓋前面的參數值==
- 省略dest參數
如果省略了dest參數,該方法就只能有一個src參數但狭,而且是將該src合并到調用extend方法的對象中
$.extend(src) //該方法就是將src合并到jquery全局對象中
$.fn.extend(src) //該方法將src合并到jquery實例對象中
- extend的重載原型
extend(boolean,dest,src1,src2,src3...)
第一個參數boolean代表是否進行深拷貝披诗,其余參數同上
var result = $.extend(true,{},
{name: 'nnn',location: {city: 'Boston',country: 'USA'}},
{last: 'mmm',location: {state: "MA",country: 'China'}})
//深拷貝結果:
result = {name: 'nnn', last: 'mmm', location: { city: 'Boston', state: 'MA', country: 'China'}
var result = $.extend(false,{},
{name: 'nnn',location: {city: 'Boston',country: 'USA'}},
{last: 'mmm',location: {state: "MA",country: 'China'}})
//淺拷貝結果:
result = {name: 'nnn', last: 'mmm', location: { state: 'MA', country: 'China'}