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;
      }//在火狐20以下版本,如果操作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 ) {  //严格模式下,不能使用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 );
  },

  ......

})

加油!

最新文章

  1. 解决 Tomcat Server in Eclipse unable to start within 45 seconds 不能启动的问题
  2. 手机站使图片高度统一jq代码
  3. Struts 笔记 内部资料 请勿转载 谢谢合作
  4. 【转载】桥接Microsoft Word和浏览器
  5. ACM 数独
  6. ubuntu下手把手教你搭建SVN服务器
  7. HNU 12817 Shipura(表达式求值)
  8. echarts -01 入门
  9. shell 中awk、if while 例子
  10. JS 截取字符串函数
  11. Windows Shell(外壳)编程相关
  12. linkin大话设计模式--命令模式
  13. 1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
  14. mac charles抓安卓(小米)http包
  15. 事务的ACID特性(转)
  16. grid - 隐式命名网格线名称
  17. POJ - 1321 棋盘问题 简单搜索 dfs 格子
  18. 实验十一 团队作业7—团队项目设计完善&amp;编码测试
  19. POJ2456 Aggressive cows 二分
  20. Sublime Text 使用方法

热门文章

  1. [hdu2665]Kth number(划分树求区间第k大)
  2. Python Beautiful Soup 解析库的使用
  3. 虚拟化技术:Xen与KVM的对比
  4. 15-糗事百科(python+xpath)
  5. 53-C++ CH08 01
  6. SQL Pretty Printer不错的sql格式化工具
  7. 魔法变量*args 和 **kwargs
  8. mybaties 一对多关系映射
  9. code1225 八数码Bfs
  10. explain分析sql效率