日期:2014-05-16  浏览次数:20367 次

jquery,extjs中的extend用法小结

源自:http://jackyrong.iteye.com/blog/1692150

?

?

在jquery中,extend其实在做插件时还是用的比较多的,今天同时小结jquery和ext js中
的extend用法,先来看jquery中的。
1)? extend(dest,src1,src2,src3...);?

var start = {      
 id: 123,     
  count: 41,     
  desc: 'this is information',     
  title: 'Base Object',     
  tag: 'uncategorized',     
  values: [1,1,2,3,5,8,13]};   
var more = {    name: 'Los Techies',    tag: 'javascript'};   
var extra = {    count: 42,    title: null,    desc: undefined,    values: [1,3,6,10]};   
var extended = $.extend(start, more, extra);   
console.log(JSON.stringify(extended));  

?

var start = {   
 id: 123,  
  count: 41,  
  desc: 'this is information',  
  title: 'Base Object',  
  tag: 'uncategorized',  
  values: [1,1,2,3,5,8,13]};
var more = {    name: 'Los Techies',    tag: 'javascript'};
var extra = {    count: 42,    title: null,    desc: undefined,    values: [1,3,6,10]};
var extended = $.extend(start, more, extra);
console.log(JSON.stringify(extended));


? 输出结果为:
{??? "id": 123,?
? "count": 42,??
"desc": "this is information",?
? "title": null,
?? "tag": "javascript",??
"values": [1, 3, 6, 10],??
"name": "Los Techies"}

??? 可以看到,其实是

??? extend(dest,src1,src2,src3...);

?? ,将src1,src2,src3...合并到dest中,返回值为合并后的dest,由此可以看出该方法合并后,是修改了dest的结构的。如果想要得到合并的结果却又不想修改dest的结构,可以如下使用:

? var newSrc=$.extend({},src1,src2,src3...)//也就是将"{}"作为dest参数。
比如:
? var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})


????? 那么合并后的结果

? result={name:"Jerry",age:21,sex:"Boy"}

????? 也就是说后面的参数如果和前面的参数存在相同的名称,那么后面的会覆盖前面的参数值。
??? 同时要注意的是,在第一个例子中, "desc": undefined并不会出现在结果中,
合拼的时候,依然保留了desc的原来的值。但title:null的话,会出现在extend的结果
中。

2) 其他jquery extend的用法
??? 1、$.extend(src)
  该方法就是将src合并到jquery的全局对象中去,如:

$.extend({? hello:function(){alert('hello');}? });

  就是将hello方法合并到jquery的全局对象中。
  2、$.fn.extend(src)
  该方法将src合并到jquery的实例对象中去,如:

$.fn.extend({? hello:function(){alert('hello');} });


   就是将hello方法合并到jquery的实例对象中。

  下面例举几个常用的扩展实例:

$.extend({net:{}});


   这是在jquery全局对象中扩展一个net命名空间。

? $.extend($.net,{?? hello:function(){alert('hello');}? })

?   这是将hello方法扩展到之前扩展的Jquery的net命名空间中去。



??? 3 深度复制
????? // 以前的 .extend()??
?? jQuery.extend(? false,
???? { name: “John”, location: { city: “Boston” } },??
???? { last: “Resig”, location: { state: “MA” } }??
?? );??
??? // 结果:??
??? // => { name: “John”, last: “Resig”, location: { state: “MA” } }


?? jQuery.extend( true,??
?? { name: “John”, location: { city: “Boston” } },??
???? { last: “Resig”, location: { state: “MA” } }??
? );??
? // 结果??
?? // => { name: “John”, last: “Resig”,??
? //????? location: { city: “Boston”, state: “MA” } }

3) 如果是ext js的话,看下有什么不同:?
???

var start = {     
  id: 123,    
   count: 41,     
  desc: 'this is information',   
    title: 'Base Object',    
   tag: 'uncategorized',     
  values: [1,1,2,3,5,8,13]};   
var more = {    name: 'Los Techies',    tag: 'javascript'};   
var extra = {    count: 42,    title: null,    desc: undefined,    
   values: [1,3,6,10]};   
var extended = Ext.apply(start, more, extra);console.log(JSON.stringify(extended));  

?

var start = {  
  id: 123, 
   count: 41,  
  desc: 'this is information',
    title: 'Base Object', 
   tag: 'uncategorized',  
  values: [1,1,2,3,5,8,13]};
var more = {    name: 'Los Techies',    tag: 'javascript'};
var extra = {    count: 42,    title: nul