jquery 插件開發(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...);
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的話,看下有什么不同:
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è)要注意
的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è)要注意
您可能感興趣的文章:
- jQuery插件開發(fā)的兩種方法及$.fn.extend的詳解
- 開發(fā)插件的兩個(gè)方法jquery.fn.extend與jquery.extend
- 詳解jQuery插件開發(fā)中的extend方法
- jquery插件制作 手風(fēng)琴Panel效果實(shí)現(xiàn)
- jquery插件制作 圖片走廊 gallery
- jquery插件制作 表單驗(yàn)證實(shí)現(xiàn)代碼
- 一個(gè)簡(jiǎn)單的jQuery插件制作 學(xué)習(xí)過(guò)程及實(shí)例
- 一步一步制作jquery插件Tabs實(shí)現(xiàn)過(guò)程
- jquery tab插件制作實(shí)現(xiàn)代碼
- 基于jquery插件制作左右按鈕與標(biāo)題文字圖片切換效果
- jquery簡(jiǎn)單插件制作(fn.extend)完整實(shí)例
相關(guān)文章
jquery實(shí)現(xiàn)的點(diǎn)擊翻書效果代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)的點(diǎn)擊翻書效果代碼,可呈現(xiàn)點(diǎn)擊圖片顯示翻頁(yè)的效果,涉及jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)改變頁(yè)面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
jQuery Study Notes學(xué)習(xí)筆記 (二)
jquery簡(jiǎn)單選擇器的使用方法。jquery中最基本的東西了。2010-08-08
jquery實(shí)現(xiàn)圖片隨機(jī)排列的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)圖片隨機(jī)排列的方法,涉及jQuery操作圖片的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
基于jquery的仿百度的鼠標(biāo)移入圖片抖動(dòng)效果
在鼠標(biāo)放到圖片上, 圖片會(huì)有個(gè)上下抖動(dòng)的特效。很喜歡這種感覺(jué)。在于是摸索了一下,下面是一些步驟。2010-09-09

