jquery源码学习(二)——jquery中的变量
jquery在 21-93 行提供了变量
1 var 2 // A central reference to the root jQuery(document) 3 rootjQuery, 4 5 // The deferred used on DOM ready 6 readyList, 7 8 // Support: IE9 9 // For `typeof xmlNode.method` instead of `xmlNode.method !== undefined` 10 core_strundefined = typeof undefined, 11 12 // Use the correct document accordingly with window argument (sandbox) 13 location = window.location, 14 document = window.document, 15 docElem = document.documentElement, 16 17 // Map over jQuery in case of overwrite 18 _jQuery = window.jQuery, 19 20 // Map over the $ in case of overwrite 21 _$ = window.$, 22 23 // [[Class]] -> type pairs 24 class2type = {}, 25 26 // List of deleted data cache ids, so we can reuse them 27 core_deletedIds = [], 28 29 core_version = "2.0.3", 30 31 // Save a reference to some core methods 32 core_concat = core_deletedIds.concat, 33 core_push = core_deletedIds.push, 34 core_slice = core_deletedIds.slice, 35 core_indexOf = core_deletedIds.indexOf, 36 core_toString = class2type.toString, 37 core_hasOwn = class2type.hasOwnProperty, 38 core_trim = core_version.trim, 39 40 // Define a local copy of jQuery 41 jQuery = function( selector, context ) { 42 // The jQuery object is actually just the init constructor 'enhanced' 43 return new jQuery.fn.init( selector, context, rootjQuery ); 44 }, 45 46 // Used for matching numbers 47 core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source, 48 49 // Used for splitting on whitespace 50 core_rnotwhite = /\S+/g, 51 52 // A simple way to check for HTML strings 53 // Prioritize #id over <tag> to avoid XSS via location.hash (#9521) 54 // Strict HTML recognition (#11290: must start with <) 55 rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/, 56 57 // Match a standalone tag 58 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, 59 60 // Matches dashed string for camelizing 61 rmsPrefix = /^-ms-/, 62 rdashAlpha = /-([\da-z])/gi, 63 64 // Used by jQuery.camelCase as callback to replace() 65 fcamelCase = function( all, letter ) { 66 return letter.toUpperCase(); 67 }, 68 69 // The ready event handler and self cleanup method 70 completed = function() { 71 document.removeEventListener( "DOMContentLoaded", completed, false ); 72 window.removeEventListener( "load", completed, false ); 73 jQuery.ready(); 74 };
一次性声明逗号隔开
rootJQuery
//25行中的 rootJQuery 在 866 行 = jQuery(document)
//A central reference to the root jQuery(document) 意思这是jquery最核心的概念,jQuery(document) 就是把选择到的东西保存起来。
//保存起来的原因,首先原因 1. 便于压缩 2. 语义化 3. 可维护
readyList
//和Dom加载相关的对象数组;在集合中保存中要操作的DOM元素。
// The deferred used on DOM ready
core_strundefined
//字符串类型的undefined,typeof window.a == "undefined" 这种判断方法的兼容性更好。
location documentElem document
//字符串类型的undefined,typeof window.a == "undefined" 这种判断方法的兼容性更好。
// Use the correct document accordingly with window argument (sandbox)
location = window.location,
document = window.document,
docElem = document.documentElement,
_Jquery 和 _$
//为了防止命名冲突,把以前的jquery和$保存起来到_jquery和_$, 如果没有命名冲突,那么他们就是undefined
core_deletedId 和 core_version
//这两个是和数据缓存有关系的数据,包括下边的core_开头的其他数据,在此不作解释
jQuery
//返回一个初始化的 jquery 对象
//return new jQuery.fn.init( selector, context, rootjQuery );
//jQuery.fn = jQuery.prototype
//jQuery.fn.init.prototype = jQuery.prototype ;
//所以其实jquery.fn.init其实返回一个jquery对象 , jquery.fn.int和jquery是一样的。
正则匹配
//core_pnum : 匹配各种数字
//rquickExpr : 匹配标签样式
//rsingleTag : 匹配单标签样式
//rmsPeofixed : 匹配ie样式
completed 和 fcamelCase
//前者在 DOM 加载完成后的ready();
//后者返回大写