jQuery $.extend()用法總結

來源:互聯網
上載者:User

標籤:prot   color   tip   靜態   john   style   play   size   方法   

1.合并多個對象

var Css1={size: "10px",style: "oblique"} var Css2={size: "12px",style: "oblique",weight: "bolder"} $.jQuery.extend(Css1,Css2) //結果:Css1的size屬性被覆蓋,而且繼承了Css2的weight屬性 // Css1 = {size: "12px",style: "oblique",weight: "bolder"} 

 

2.深度嵌套對象

jQuery.extend( { 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.可以給jQuery添加靜態方法。 

 

$.extend({ add:function(a,b){return a+b;}, minus:function(a,b){return a-b}, multiply:function(a,b){return a*b;}, divide:function(a,b){return Math.floor(a/b);} }); var sum = $.add(3,5)+$.minus(3,5)+$.multiply(3,5)+$.divide(5,7); console.log(sum); 

4.

jQuery為開發外掛程式提拱了兩個方法,分別是: 
jQuery.fn.extend(object);  //擴充執行個體
jQuery.extend(object);  //擴充靜態方法

$.fn.extend({name:function(){console.log("1")},age:12})a=$(‘body‘)a.name() //1

ps: 擴充外掛程式還有種簡單的方式,如下,因為jQuery 中 jQuery.fn.init.prototype = jQuery.fn;

$.fn.display=function{}

 

 

 

jQuery $.extend()用法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.