jQuery源码阅读笔记(21-94行)

来源:互联网 发布:firework软件 编辑:程序博客网 时间:2024/04/30 15:46

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 readyreadyList,//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 pairsclass2type = {},//用于判断变量类型,最终值为{'[Object String]':'String','[Object Array]':'array'...}类型的,所以为class  to  type// List of deleted data cache ids, so we can reuse themcore_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 jQueryjQuery = 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注入木马,匹配一个标签或#idrquickExpr = /^(?:\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();};


1 0
原创粉丝点击