jquery源碼筆記(四): jquery.extend=jquery.fn.extend

來源:互聯網
上載者:User

標籤:

extend()  方法,   合并兩個或更多個物件的屬性到第一個對象中,jQuery後續的大部分功能都通過該函數擴充,

當參數只有一個對象時,則將對象的屬性添加到jQuery對象中。

jquery 中擴充外掛程式的形式:  2種方法

$.extend({                //擴充工具方法        aaa:function(){            alert(10);        },        bbb:function(){            alert(20)        }    });    $.fn.extend({            //擴充jquery執行個體方法        aaa:function(){            alert("fn  1");        },        bbb:function(){            alert("fn  2");        }    });    $.extend() ; this  -> $   this.aaa  ->  $.aaa();    $.fn.extend();   this -> $.fn (原型)  this.aaa  -> $().aaa()

當參數中有多個對象的時候,後面的對象都是擴充到第一個對象上:

    var a = {name:"Linda"};    $.extend(a,{name:"Joke",age:20},{address:"China"});    console.log(a);        //    { name="Joke",  age=20,  address="China"}

還有  深拷貝 和 淺拷貝  , jquery預設是淺拷貝  

淺拷貝:var a = {name:"Linda"};    var b = {firends:[‘aa‘,‘bb‘,‘cc‘]};    $.extend(a,b);    a.firends.push(‘dd‘);    console.log(b);        //    { firends:[‘aa‘,‘bb‘,‘cc‘,‘dd‘]}深拷貝:var a = {name:"Linda"};    var b = {firends:[‘aa‘,‘bb‘,‘cc‘]};    $.extend(true,a,b);  //第一個參數設定為 true , 那就是 深拷貝    a.firends.push(‘dd‘);  此時a 的參考型別的 修改對  b 沒有影響    console.log(b);        //    { firends:[‘aa‘,‘bb‘,‘cc‘]}

源碼的 分解:

 jQuery.extend = jQuery.fn.extend = function() {    定義一些變數
  if(){} 看看是不是深拷貝情況
  if(){}  看看參數正確不
  if(){} 看看是不是外掛程式情況    for(){ 可能有多個對象情況
      if(){} 防止循環參考
      if(){} 深拷貝
       else if(){} 淺拷貝
  }

 

jquery源碼筆記(四): jquery.extend=jquery.fn.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.