jQuery源码学习之二 (部分变量解释说明)

几个变量的相关说明:

1、rootjQuery

后面的代码中可以找到  rootjQuery = jQuery(document);  赋予一个变量后,一是便于压缩;二是语义化后更容易以后的代码中使用理解,可维护性强

2、readyjQuery

3、core_strundefined = typeof undefined

xml中直接判断一个变量是不是等于undefined 在ie9及以下版本中会判断不到,需用typeof判断。所以存储了字符串形式的 “undefined"

   a == undefined;            //xml下 ie中无效
   typeof a == "undefined"    //有效
4、便于压缩
   location = window.location,
   document = window.document,  //document
   docElem = document.documentElement,  //html
5、防冲突函数 noConflict 中使用
   // Map over jQuery in case of overwrite
   _jQuery = window.jQuery,
   // Map over the $ in case of overwrite 
   _$ = window.$,

 防冲突使用示例

   //示例一:
   <script src="jquery-2.0.3.js"></script>
   var $1 = jQuery.noConflict(true);
   var jQuery = 7;

   //若是以上使用方法,jQuery中直接写下面简单的就行了
   (function (window, undefined) {
      var jQuery = function () {
        //...
       };
     jQuery.noConflict = function (deep) {
         return jQuery;
      };
   })(window);



 为了也同时兼容下面这种情况
   //示例二:
   var jQuery = 7;
   <script src="jquery-2.0.3.js"></script>
   var $1 = jQuery.noConflict(true);

   //jQuery中写成这样
   (function (window, undefined) {
      var _jQuery = window.jQuery,
           _$ = window.$,
          jQuery = function () {
            //...
           };
      jQuery.noConflict = function (deep) {
         if (window.$ === jQuery) {
             window.$ = _$;
         };
         if (deep && window.jQuery === jQuery) {
            window.jQuery = _jQuery;
         };
         return jQuery;
      };
      if (typeof window === "object" && typeof window.document === "object") {
         window.jQuery = window.$ = jQuery;
      };
   })(window);


6 、“ ”.trim() 新版本浏览器js自带方法

7、jQuery构造函数的声明

var jQuery = function( selector, context ) 
     //内部返回jQuery.fn.init方法的实例,所以我们调用的时候不用再写new 
     return new jQuery.prototype.init( selector, context, rootjQuery );     
    };
jQuery.prototype.init = function( selector, context, rootjQuery ){   //初始化方法
    //...
    };
jQuery.prototype.css = function( selector, context, rootjQuery ){    //其它实例方法
     //...
    };
//下面代码使挂载到jQuery.prototype的方法同样可以应用于jQuery.prototype.init的实例上
//所以我们调用方法时可以 :$("...").css();
jQuery.prototype.init.prototype = jQuery.prototype;   



相关 jQuery源码:

var
// A central reference to the root jQuery(document)
rootjQuery,

// The deferred used on DOM ready
readyList,

// Support: IE9
// For `typeof xmlNode.method` instead of `xmlNode.method !== undefined`
core_strundefined = typeof undefined,

// Use the correct document accordingly with window argument (sandbox)
location = window.location,
document = window.document,
docElem = document.documentElement,

// Map over jQuery in case of overwrite
_jQuery = window.jQuery,

// Map over the $ in case of overwrite
_$ = window.$,

// [[Class]] -> type pairs
class2type = {},

// List of deleted data cache ids, so we can reuse them
core_deletedIds = [],

core_version = "2.0.3",

// Save a reference to some core methods
core_concat = core_deletedIds.concat,
core_push = core_deletedIds.push,
core_slice = core_deletedIds.slice,
core_indexOf = core_deletedIds.indexOf,
core_toString = class2type.toString,
core_hasOwn = class2type.hasOwnProperty,
core_trim = core_version.trim,

// Define a local copy of jQuery
jQuery = function( selector, context ) {
	// The jQuery object is actually just the init constructor 'enhanced'
	return new jQuery.fn.init( selector, context, rootjQuery );
},

// Used for matching numbers
core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,

// Used for splitting on whitespace
core_rnotwhite = /\S+/g,

// A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
// Strict HTML recognition (#11290: must start with <)
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,

// Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,

// Matches dashed string for camelizing
rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi,

// Used by jQuery.camelCase as callback to replace()
fcamelCase = function( all, letter ) {
	return letter.toUpperCase();
},

// The ready event handler and self cleanup method
completed = function() {
	document.removeEventListener( "DOMContentLoaded", completed, false );
	window.removeEventListener( "load", completed, false );
	jQuery.ready();
};

posted @ 2014-07-11 11:29  hdchangchang  阅读(230)  评论(0编辑  收藏  举报