jQuery(版本2.0.3)在21 - 94行中定义了一些变量和函数。
下载地址:https://code.jquery.com/jquery/
版权声明:以下为本人在妙味课堂听课的笔记
//"use strict";//采用严格模式,不建议,因为存在些兼容性问题,且在.net中存在追踪问题,在火狐中容易造成假死现象
var
// A central reference to the root jQuery(document)
rootjQuery,//指向document,在866行中为其赋值,作用同window形参
// The deferred used on DOM ready
readyList,//DOM元素操作集
// 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,//存储一些常用变量,有利于压缩
//变量_jQuery _$是为了防止与其他库中变量名冲突
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$,
// [[Class]] -> type pairs
class2type = {},//用于判断变量类型,最终值为{'[Object String]':'String','[Object Array]':'array'...}类型的,所以为class to type
// 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,
//jQuery为对外提供的接口,在此进行初始化,返回一个新建的对象
/*通常我们定义面向对象的方式如下:
function Aaa(){
}
Aaa.prototype.init = function(){
};
Aaa.prototype.css = function(){
};
所以在使用时我们一般需要先new出来一个对象,再调用init将其初始化,才能使用新建的对象
var a1 = new Aaa();
a1.init();
a1.css();
而在jQuery中,定义jQuery时直接执行init初始化函数,使得我们在使用时可以直接采用类似$('div')的方法而不需要再new
但是这样处理,new出来的是一个叫jQuery.fn.init的对象,而原型挂载的方法需要挂载到对象jQuery下,所以下文中有这样两行代码进行处理
jQuery.fn = jQuery.prototype;
jQuery.fn.init.prototype = jQuery.fn;
通过对象的赋值将jQuery.prototype的值赋予jQuery.fn.init
*/
// 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 );
},
//关于下面的正则,提供一个非常方便的小工具 https://regexper.com/ ,输入正则表达式,会显示相应的图形解释,帮助我们理解正则表达式的意思
//匹配数字
core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,
//匹配单词
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 <)
//防止通过url后边加入XSS注入木马,匹配一个标签或#id
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
// Match a standalone tag匹配成对的标签
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
// Matches dashed string for camelizing,用于转成驼峰的特殊处理
//浏览器内核,因为ie比较特殊,所以做专门处理。
//-webkit-margin-left : webkitMarginLeft
//-ms-margin-left : MsMarginLeft
//特殊的还有数字开头的
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,DOM加载的回调方法
completed = function() {
document.removeEventListener( "DOMContentLoaded", completed, false );
window.removeEventListener( "load", completed, false );
jQuery.ready();
};
本文详细解析了jQuery 2.0.3版本的初始化过程,介绍了核心变量和函数的定义,如rootjQuery、readyList等,并对部分关键正则表达式的用途进行了说明。
&spm=1001.2101.3001.5002&articleId=53423186&d=1&t=3&u=42223e670602413b8683bd944a488694)
225

被折叠的 条评论
为什么被折叠?



