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();
};

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jquery中文汉化版 (function( window, undefined ) { //不要做这个因为各自的应用程序包括ASP.NET查找 // the stack via arguments.caller.callee and Firefox dies if //你尝试查找通过“精确使用”呼叫链接(#13335) //支持:火狐浏览器 18+ //“精确使用”; var //deferred对象被使用在DOM(Document Object Model翻译:文档对象模型)准备之时 //deferred(延迟)对象:从jQuery 1.5.0版本开始引入的一个新功能 //在DOM准备好时调用 readyList, //一个中心引用对于jQuery根文档 //对根jQuery对象的主要引用 rootjQuery, //支持:IE9之前的版本 // For `typeof node.method` instead of `node.method !== undefined` core_strundefined = typeof undefined, // Use the correct document accordingly with window argument (sandbox) document = window.document,//window文档赋值给变量document location = [removed], // Map over jQuery in case of overwrite(不确定,待修正,希望高人帮忙翻译一下) //在jQuery上绘制写在上面的实例 //防止被覆盖 _jQuery = window.jQuery, // Map over the $ in case of overwrite _$ = window.$, //将window正则表达式符号$赋值给变量_$ //[类]:成双类型 class2type = {}, //在贮存区被删除数据ID的列表,我们能够再用他们 core_deletedIds = [], core_version = "1.9.1", //保存一个参考给一些核心的方法 //为核心方法创建引用 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本地代码 //构建jQuery对象 jQuery = function( selector, context ) { //jQuery对象是实际上初始化名为enhanced(提高的)构造器 //jQuery对象实际上只是增强的初始化构造方法 return new jQuery.fn.init( selector, context, rootjQuery ); }, /* 用来匹配数字的正则,匹配可选正负号、浮点型、整型、科学计数法 * 没有使用(?)来表示可选而是通过(|)来选择 * (?:\d*\.|)匹配浮点数时,|前的\d*\.可以匹配整数部分和小数点,小数部分由后面的\d+匹配 * 匹配整数时,|)可以保证匹配继续向下进行,整数由后面的\d+匹配,同样的\d+在匹配整型和浮点型时负责的匹配部分不同 * [eE][\-+]?\d+|)处理科学计数法的匹配,同样没有使用?表示可选 */ core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source, //用于分开空格 core_rnotwhite = /\S+/g, //查找非空白字符串 // Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE) rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, //\uFEFF:字节顺序标志 //一个简单途径用于检查HTML字符串 // Prioritize #id over <tag> to avoid XSS via location.hash (#9521) // Strict HTML recognition (#11290: must start with <) rquickExpr = /^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/, //匹配一个独立的标签 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, // JSON RegExp(JavaScript Object Notation:JavaScript对象标记法正则表达式) rvalidchars = /^[\],:{}\s]*$/, rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g, rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g, rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g, // Matches dashed string for camelizing rmsPrefix = /^-ms-/, rdashAlpha = /-([\da-z])/gi, //以上为正则运算表达式各种形式,不太容易理解,尽量掌握。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值