jquery源碼解析:jQuery工具方法詳解2

來源:互聯網
上載者:User

標籤:des   style   io   ar   color   os   使用   sp   for   

jQuery的工具方法,其實就是靜態方法,源碼裡面就是通過extend方法,把這些工具方法添加給jQuery建構函式的。

jQuery.extend({ 

  ......  

  type: function( obj ) {    //$.type(),判斷類型
    if ( obj == null ) {   //null,undefined
      return String( obj );    //返回null,undefined字串
    }     //core_toString = {}.toString 
    return typeof obj === "object" || typeof obj === "function" ?
      class2type[ core_toString.call(obj) ] || "object" :     //number,string等封裝類型,typeof判斷是object,所以進入到toString判斷,最終返回number,string

        typeof obj;       //基本類型,number,string直接用typeof.
  }, 

  isPlainObject: function( obj ) {   //判斷是否是對象自變數,{},new Object
    if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
      return false;    //dom節點,window等是object,但是不是對象自變數。
    }

    try {     //window.location是object,core_hasOwn是{}.hasOwnProperty(自己的屬性,不是原型的屬性)
      if ( obj.constructor && !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
        //isPrototypeOf只存在object.prototype對象中,core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" )這句話的意思就是:obj.constructor.prototype是否有isPrototypeOf屬性,從上可知,只有object.prototype有,所以只有object.prototype才返回真。比如,我傳入一個Person(建構函式),它的原型沒有isPrototypeOf屬性,所以返回false。Person不是對象自變數。

        return false;
      }//在Firefox20以下版本,如果操作window.location.constructor 多次,會拋錯
    } catch ( e ) {
      return false;
    }

    return true;
  },

  isEmptyObject: function( obj ) {  //{},[],對象自身下沒有屬性和方法就返回true。function Person(){} ;var person = new Person(),返回true

    var name;
    for ( name in obj ) {     //系統內建的屬性和方法不會for in列印出來。
      return false;
    }
    return true;
  }, 

  parseHTML: function( data, context, keepScripts ) {   //解析字串為數組節點,比如:"<li></li>" -> [li元素]

    //keepScripts 為true,可以解析script,false不能解析script標籤
    if ( !data || typeof data !== "string" ) {  
      return null;
    }
    if ( typeof context === "boolean" ) {   //如果沒有第二個參數
      keepScripts = context;
      context = false;
    }
    context = context || document;

    var parsed = rsingleTag.exec( data ),     //假如是單標籤"<li></li>"
    scripts = !keepScripts && [];

    if ( parsed ) {    //直接建立這個元素,然後放進數組中返回
      return [ context.createElement( parsed[1] ) ];
    }

    parsed = jQuery.buildFragment( [ data ], context, scripts );  

     //如果keepScripts為false,則會傳入空數組scripts ,buildFragment會判斷data中是否有script,如果有就放入空數組,變成[script],沒有就返回[]。如果keepScripts為true,就傳入false,buildFragment不對它做改變,還是false.

    if ( scripts ) {
      jQuery( scripts ).remove();   //移除script標籤
    }

    return jQuery.merge( [], parsed.childNodes );
  },//eval可以解析json字串,任何格式的都可以,危險性提高。JSON.parse()只能解析標準的json字串,效能好於eval。 

  parseXML: function( data ) {  //解析xml字串為xml文檔。
    var xml, tmp;
    if ( !data || typeof data !== "string" ) {
      return null;
    }

    try {
      tmp = new DOMParser();    //IE6-8使用ActiveXObject來解析
      xml = tmp.parseFromString( data , "text/xml" );

      //IE9下,如果xml字串不是正常的xml標籤,會報錯。其他瀏覽器不會報錯,會在返回的xml文檔中產生parsererror節點
    } catch ( e ) {
      xml = undefined;
    }

    if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) {
      jQuery.error( "Invalid XML: " + data );
    }
    return xml;
  }, 

  globalEval: function( code ) {   //把局部變數變成全域變數,比如:"var a=1"。
    var script,
    indirect = eval;//eval既是關鍵字,又是window下的屬性。直接寫eval(code),瀏覽器會當做js關鍵字使用,出錯。所以賦值一下,就會把eval當做window的屬性。

    code = jQuery.trim( code );      //code必須為字串形式

    if ( code ) {
      if ( code.indexOf("use strict") === 1 ) {  //strict 模式下,不能使用eval.
        script = document.createElement("script");
        script.text = code;
        document.head.appendChild( script ).parentNode.removeChild( script );
      } else {
        indirect( code );
      }
    }
  },  

  camelCase: function( string ) {   //轉駝峰,IE下:-ms-transform -> msTransform,而其他-moz-transform-> MozTransform,所以先把-ms-替換成ms-,這樣就只會轉成msTransform,m不會大寫了。而其他瀏覽器需要大寫
    return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
  },

  ......

})

 

加油!

jquery源碼解析:jQuery工具方法詳解2

聯繫我們

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