文章出處

一、jquery源碼1-60行

該部分代碼主要完成jquery對象的創建,以及全局變量$與jQurey類的映射;

/*
 * jQuery - New Wave Javascript
 *
 * Copyright (c) 2006 John Resig (jquery.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt) 
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2006-10-27 23:14:48 -0400 (Fri, 27 Oct 2006) $
 * $Rev: 509 $
 */

// Global undefined variable
window.undefined = window.undefined;
function jQuery(a,c) {

    // Shortcut for document ready (because $(document).each() is silly)
    if ( a && a.constructor == Function && jQuery.fn.ready )
        return jQuery(document).ready(a);

    // Make sure that a selection was provided
    a = a || jQuery.context || document;

    // Watch for when a jQuery object is passed as the selector
    if ( a.jquery )
        return $( jQuery.merge( a, [] ) );

    // Watch for when a jQuery object is passed at the context
    if ( c && c.jquery )
        return $( c ).find(a);
    
    // If the context is global, return a new object
    if ( window == this )
        return new jQuery(a,c);

    // Handle HTML strings
    var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
    if ( m ) a = jQuery.clean( [ m[1] ] );

    // Watch for when an array is passed in
    this.get( a.constructor == Array || a.length && !a.nodeType && a[0] != undefined && a[0].nodeType ?
        // Assume that it is an array of DOM Elements
        jQuery.merge( a, [] ) :

        // Find the matching elements and save them for later
        jQuery.find( a, c ) );

  // See if an extra function was provided
    var fn = arguments[ arguments.length - 1 ];
    
    // If so, execute it in context
    if ( fn && fn.constructor == Function )
        this.each(fn);
}

// Map over the $ in case of overwrite
if ( $ )
    jQuery._$ = $;

// Map the jQuery namespace to the '$' one
var $ = jQuery;

二、關于window.undefined

window.undefined = window.undefined;

這樣寫無論window有沒定義undefined,window.undefined都能正確表示它的意思,在有些早期瀏覽器中window并沒有定義undefined變量,所以jquery1.0中這樣寫;

三、關于ready函數

if ( a && a.constructor == Function && jQuery.fn.ready )
        return jQuery(document).ready(a);

ready方法為jquery對象的原型方法,在源文件1093行中利用jquery的extend方法擴展的;

jquery中ready函數有兩種寫法:

1、直接調用原型方法ready

$(document).ready(function(){ /* do something */ }) 

2、直接往jQuery方法傳入function對象

$(function(){ /* do something */ })  

三、a = a || jQuery.context || document;

確保選中一個對象,如果a和jQuery.context都沒定義,則會返回一個封裝了document的jquery對象,如下:

var $obj = $();         //$obj等同于$(document)

四、jquery對象作為參數傳入

    if ( a.jquery )
        return $( jQuery.merge( a, [] ) );

注意每個jquery對象都有jquery屬性,值為它的版本號,因此可用此屬性判斷對象是否是jquery對象;

如果是jquery對象,則返回一份它的拷貝,注意jquery對象里面的DOM對象指的還是同一個引用,其它屬性有各自的空間,具體可看merge和get方法內部實現,后續會討論到;

例:   

    var objTmp = $("<p>23</p>");
    var objTmp2 = $(objTmp);
    objTmp2.jquery = 'ss';
    var test1 = objTmp.jquery;       //test1="$Rev: 509 $"
    var test2 = objTmp2.jquery;      //test2="ss"


    var objTmp = $("123<p>23</p>sds");
    var objTmp2 = $(objTmp);
    objTmp2.html("hello");
    var test1 = objTmp.html();       //test1 = "hello"
    var test2 = objTmp2.html();      //test2 = "hello"

五、返回某jquery對象context下的jquery對象

    if ( c && c.jquery )
        return $( c ).find(a);

例:   

var objTmp4 = $(".testDIv", $obj); //返回$obj節點下所有clss為testDIv的jquery對象

六、new jQuery

 if ( window == this )
        return new jQuery(a,c);

如果context是全局的,則new一個jquery對象并返回;

例如:$("#id");     

該情況context是全局的,第一次運行到該語句,條件成立(window==this),因為js代碼都是在window作用于下運行的,第二次運行到該語句的時候,jquery對象已經new出來了,此時this為new出來的對象,條件不成立(window!=this),跳過;

七、匹配處理html字符串

    var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
    if ( m ) a = jQuery.clean( [ m[1] ] );

注意第一句, 該正則表達式^[^<]*(<.+>)[^>]*$,

[^<]  *       //匹配n個非<字符,

(<.+>)       // 匹配<除“\r\n”之外的任何單個字符>

[^>]*        //匹配n個非>字符,

第二句,當匹配成功時,調用clean函數處理,參數為尖括號里面的字符串,clean函數通過document.createElement("div")創建一個臨時div對象,然后將參數賦給innerHTML,最后將臨時div的childNodes壓入數組返回給a;

八、為jquery對象里的Dom對象、length賦值

 this.get( a.constructor == Array || a.length && !a.nodeType && a[0] != undefined && a[0].nodeType ?
        jQuery.merge( a, [] ) : jQuery.find( a, c ) );

往get函數傳入數組或者類數組對象,get方法通過該數組參數生成Dom對象和length值,關于這部分的get源碼如下

   this.length = 0;
   [].push.apply( this, num );
   return this;

簡單的說,就是在jquery對象中push進數組中的dom對象,由于jquery對象通過apply調用數組中的push方法,length也自動++;

push完成后,該jquery對象就擁有了length和一系列dom元素(如果有的話);

通過調用merge和find函數,會返回數組;

九、最后一個參數是函數的情況

    var fn = arguments[ arguments.length - 1 ];
   
    if ( fn && fn.constructor == Function )
        this.each(fn);

如果最后一個參數是函數,則遍歷執行;

十、全局變量$

if ( $ )
    jQuery._$ = $;
    var $ = jQuery; 

如果已經定義$,則將$保存到 jQuery下,防止overwrite;

將jQuery namespace映射到$下,可少寫5個字符;

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()