1. 程式人生 > >jQuery源碼分析-03構造jQuery對象-源碼結構和核心函數

jQuery源碼分析-03構造jQuery對象-源碼結構和核心函數

ear map plain instant cnblogs dom 分析 isempty func

3. 構造jQuery對象

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

jQuery源碼分析-03構造jQuery對象-源碼結構和核心函數