$(function(){ $.extend({ hello:function(){alert('hello');} //该方法只有一个参数,意味着将hello方法合并到jQuery全局对象中去 }); $.hello(); //弹出 hello })
命名空间示例:
$(function(){ $.extend({net:{}}); //扩展一个命名空间 $.extend($.net,{ hello:function(){alert('hello');} //将hello方法绑定到命名空间net里去 }) $.net.hello(); //通过net命名空间调用方法 })
拷贝方法原型:
extend(boolean,dest,src1,src2,src3...)
其中第一个参数boolean表示是否进行深层拷贝。
$(function(){ var result=$.extend( true, {}, { name: "John", location: {city: "Boston",country:"USA"} }, { last: "Resig", location: {state: "MA",country:"China"} } ); alert(result.location.state); //输出 MA //result={name:"John",last:"Resig", location:{city:"Boston",state:"MA",county:"China"}} var result=$.extend( false, {}, { name: "John", location: {city: "Boston",country:"USA"} }, { last: "Resig", location: {state: "MA",country:"China"} } ); alert(result.location.city); //输出 undefined //result={name:"John",last:"Resig",location:{state:"MA",county:"China"}} 注意没有city,只是合并了location,location里面的属性不管 })