- 扩展方法
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'}
网友评论