jQuery源碼學習筆記:構造jQuery對象

來源:互聯網
上載者:User

3.1源碼結構:

(function( window, undefined ) {       var jQuery = (function() {       // 構建jQuery對象       var jQuery = function( selector, context ) {           return new jQuery.fn.init( selector, context, rootjQuery );       }          // jQuery對象原型       jQuery.fn = jQuery.prototype = {           constructor: jQuery,           init: function( selector, context, rootjQuery ) {              // selector有以下7種分支情況:              // DOM元素              // body(最佳化)              // 字串:HTML標籤、HTML字串、#id、選取器運算式              // 函數(作為ready回呼函數)              // 最後返回偽數組           }       };          // Give the init function the jQuery prototype for later instantiation       jQuery.fn.init.prototype = jQuery.fn;          // 合并內容到第一個參數中,後續大部分功能都通過該函數擴充       // 通過jQuery.fn.extend擴充的函數,大部分都會調用通過jQuery.extend擴充的同名函數       jQuery.extend = jQuery.fn.extend = function() {};             // 在jQuery上擴充靜態方法       jQuery.extend({           // ready bindReady           // isPlainObject isEmptyObject           // parseJSON parseXML           // globalEval           // each makeArray inArray merge grep map           // proxy           // access           // uaMatch           // sub           // browser       });         // 到這裡,jQuery物件建構完成,後邊的代碼都是對jQuery或jQuery對象的擴充       return jQuery;       })();       window.jQuery = window.$ = jQuery;})(window);

3.2 jQuery.extend = jQuery.fn.extend

// 合并兩個或更多個物件的屬性到第一個對象中,jQuery後續的大部分功能都通過該函數擴充// 通過jQuery.fn.extend擴充的函數,大部分都會調用通過jQuery.extend擴充的同名函數 // 如果傳入兩個或多個對象,所有對象的屬性會被添加到第一個對象target // 如果只傳入一個對象,則將對象的屬性添加到jQuery對象中。// 用這種方式,我們可以為jQuery命名空間增加新的方法。可以用於編寫jQuery外掛程式。// 如果不想改變傳入的對象,可以傳入一個Null 物件:$.extend({}, object1, object2);// 預設合併作業是不迭代的,即便target的某個屬性是對象或屬性,也會被完全覆蓋而不是合并// 第一個參數是true,則會迭代合并// 從object原型繼承的屬性會被拷貝// undefined值不會被拷貝// 因為效能原因,JavaScript內建類型的屬性不會合并 // jQuery.extend( target, [ object1 ], [ objectN ] )// jQuery.extend( [ deep ], target, object1, [ objectN ] )jQuery.extend = jQuery.fn.extend = function() {    var options, name, src, copy, copyIsArray, clone,       target = arguments[0] || {},       i = 1,       length = arguments.length,       deep = false;     // Handle a deep copy situation    // 如果第一個參數是boolean型,可能是深度拷貝    if ( typeof target === "boolean" ) {       deep = target;       target = arguments[1] || {};       // skip the boolean and the target       // 跳過boolean和target,從第3個開始       i = 2;    }     // Handle case when target is a string or something (possible in deep copy)    // target不是對象也不是函數,則強制設定為空白對象    if ( typeof target !== "object" && !jQuery.isFunction(target) ) {       target = {};    }     // extend jQuery itself if only one argument is passed    // 如果只傳入一個參數,則認為是對jQuery擴充    if ( length === i ) {       target = this;       --i;    }     for ( ; i < length; i++ ) {       // Only deal with non-null/undefined values       // 只處理不允許為 NULL 的參數       if ( (options = arguments[ i ]) != null ) {           // Extend the base object           for ( name in options ) {              src = target[ name ];              copy = options[ name ];               // Prevent never-ending loop              // 避免循環參考              if ( target === copy ) {                  continue;              }               // Recurse if we're merging plain objects or arrays              // 深度拷貝且值是純對象或數組,則遞迴              if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {                  // 如果copy是數組                  if ( copyIsArray ) {                     copyIsArray = false;                     // clone為src的修正值                     clone = src && jQuery.isArray(src) ? src : [];                  // 如果copy的是對象                  } else {                     // clone為src的修正值                     clone = src && jQuery.isPlainObject(src) ? src : {};                  }                   // Never move original objects, clone them                  // 遞迴調用jQuery.extend                  target[ name ] = jQuery.extend( deep, clone, copy );               // Don't bring in undefined values              // 不能拷貝空值              } else if ( copy !== undefined ) {                  target[ name ] = copy;              }           }       }    }     // Return the modified object    // 返回更改後的對象    return target;};




聯繫我們

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