jquery 外掛程式開發 extjs中的extend用法小結

來源:互聯網
上載者:User

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

輸出結果為:
{ "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));

輸出:
{ "id": 123, "count": 42, "title": null, "tag": "javascript", "values": [1,3,6,10], "name": "Los Techies"}
可以看到,extjs中使用的是apply,而desc居然在合拼的結果中丟掉了,因為ext js認為undefind的東西不應該出現在合拼的結果中了,認為是擦除掉原來的值了,這個要注意

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.