亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jquery 插件開(kāi)發(fā) extjs中的extend用法小結(jié)

 更新時(shí)間:2013年01月04日 11:19:34   作者:  
在jquery中,extend其實(shí)在做插件時(shí)還是用的比較多的,今天同時(shí)小結(jié)jquery和ext js中的extend用法,有需要的朋友可以參考下
在jquery中,extend其實(shí)在做插件時(shí)還是用的比較多的,今天同時(shí)小結(jié)jquery和ext js中
的extend用法,先來(lái)看jquery中的。
1) extend(dest,src1,src2,src3...);
復(fù)制代碼 代碼如下:

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));

輸出結(jié)果為:
{ "id": 123,
"count": 42,
"desc": "this is information",
"title": null,
"tag": "javascript",
"values": [1, 3, 6, 10],
"name": "Los Techies"}
可以看到,其實(shí)是
extend(dest,src1,src2,src3...);
,將src1,src2,src3...合并到dest中,返回值為合并后的dest,由此可以看出該方法合并后,是修改了dest的結(jié)構(gòu)的。如果想要得到合并的結(jié)果卻又不想修改dest的結(jié)構(gòu),可以如下使用:
var newSrc=$.extend({},src1,src2,src3...)//也就是將"{}"作為dest參數(shù)。
比如:
var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})
那么合并后的結(jié)果
result={name:"Jerry",age:21,sex:"Boy"}
也就是說(shuō)后面的參數(shù)如果和前面的參數(shù)存在相同的名稱,那么后面的會(huì)覆蓋前面的參數(shù)值。
同時(shí)要注意的是,在第一個(gè)例子中, "desc": undefined并不會(huì)出現(xiàn)在結(jié)果中,
合拼的時(shí)候,依然保留了desc的原來(lái)的值。但title:null的話,會(huì)出現(xiàn)在extend的結(jié)果
中。
2) 其他jquery extend的用法
1、$.extend(src)
  該方法就是將src合并到j(luò)query的全局對(duì)象中去,如:
$.extend({ hello:function(){alert('hello');} });
  就是將hello方法合并到j(luò)query的全局對(duì)象中。
  2、$.fn.extend(src)
  該方法將src合并到j(luò)query的實(shí)例對(duì)象中去,如:
$.fn.extend({ hello:function(){alert('hello');} });
   就是將hello方法合并到j(luò)query的實(shí)例對(duì)象中。
  下面例舉幾個(gè)常用的擴(kuò)展實(shí)例:
$.extend({net:{}});
   這是在jquery全局對(duì)象中擴(kuò)展一個(gè)net命名空間。
$.extend($.net,{ hello:function(){alert('hello');} })
  這是將hello方法擴(kuò)展到之前擴(kuò)展的Jquery的net命名空間中去。
3 深度復(fù)制
// 以前的 .extend()
jQuery.extend( false,
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
// 結(jié)果:
// => { name: “John”, last: “Resig”, location: { state: “MA” } }
jQuery.extend( true,
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
// 結(jié)果
// => { name: “John”, last: “Resig”,
// location: { city: “Boston”, state: “MA” } }
3) 如果是ext js的話,看下有什么不同
復(fù)制代碼 代碼如下:

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));

輸出
{ "id": 123, "count": 42, "title": null, "tag": "javascript", "values": [1,3,6,10], "name": "Los Techies"}
可以看到,extjs中使用的是apply,而desc居然在合拼的結(jié)果中丟掉了,因?yàn)閑xt js認(rèn)為undefind的東西不應(yīng)該出現(xiàn)在合拼的結(jié)果中了,認(rèn)為是擦除掉原來(lái)的值了,這個(gè)要注意

相關(guān)文章

最新評(píng)論