Skip to content

Latest commit

 

History

History
234 lines (182 loc) · 4.64 KB

私有属性.md

File metadata and controls

234 lines (182 loc) · 4.64 KB

2.私有属性

2.1 rootjQuery

  • 压缩
  • 查找局部变量rootjQuery而不是执行jQuery(document),提高代码性能

源码

//[21~23]
var
	// A central reference to the root jQuery(document)
	rootjQuery,

//[865~866]
// All jQuery objects should point back to these
rootjQuery = jQuery(document);

提示: rootjQuery可以压缩,jQuery(document)不能被压缩.

2.2 readyList

  • 用于加载DOM
  • 延迟对象

详见5.3 $.ready()

2.3 core_strundefined

  • 兼容性

源码

//[28~30]
// Support: IE9
// For `typeof xmlNode.method` instead of `xmlNode.method !== undefined`
core_strundefined = typeof undefined, //'undefined'字符串

内容解析

window.a == undefined 				//并不是所有情况都兼容,xml节点不能判断 xmlNode
typeof window.a == 'undefined'		//所有情况兼容

2.4 window属性

  • 压缩
  • 缩短查找作用域链

源码

//[32~35]
// Use the correct document accordingly with window argument (sandbox)
location = window.location,
document = window.document,
docElem = document.documentElement,

2.5 _变量

  • 防冲突

源码

[37~41]
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,

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

内容解析

<script>
	var $ = 'not jQuery'; //用户自定义或者第三方库的变量
</script>

<script src='Jquery2.0.3.js'></script>
//执行了_$ = window.$, 将用户或第三方的$变量内容存储下来,防止引用jQuery之前的变量冲突
<script>

</script>

2.6 class2type

  • 空对象
  • 类型

源码

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

详见5.7 $.type()

2.7 core_deletedIds

  • 空数组

源码

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

2.8 core_version

  • 字符串
  • 版本号

源码

//[49]
core_version = "2.0.3",

2.9 数组、对象、字符串方法

  • 压缩
  • 缩短查找时间

源码

//[51~58]
// 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,	//去除字符串的空格

2.10 jQuery(重点)

  • 构造函数
  • 原型
  • 面向对象

源码

//[60]
// 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 );
},
//[96]
jQuery.fn = jQuery.prototype = {
    // The current version of jQuery being used
    jquery: core_version,
    constructor: jQuery,
    init: function( selector, context, rootjQuery ) {}
    ...
}
//[282]
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;

内容解析

(一)、普通面向对象的编程方法

function Obj() {}
Obj.prototype.init = function(){
};

Obj.prototype.extend = function(){
};
var o = new Obj();
o.init();		//首先需要初始化
o.css();		//然后才去做其他方法的工作,那么jQuery是这么做的?

(二)、jQuery的面向对象的编程方法

function jQuery() {
    return new jQuery.prototype.init();         //jQuery.prototype = jQuery.fn
    //类似于return new jQuery();
    //同时return new A()的形式让我们在创建实例时可以省略new,例如$('div'),而不是new $('div')
}
jQuery.prototype.init = function() {
    alert('init');
}
jQuery.prototype.css = function() {
    alert('css');
}
jQuery.prototype.init.prototype = jQuery.prototype; //jQuery.prototype.init = jQuery
jQuery().css(); //init css
//jQuery()返回jQuery实例的同时进行初始化工作
//jQuery()类似于var a = new A(); a.init(); 两步操作

2.11 正则变量

源码

//[66]
// 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,

2.12 fcamelCase

  • 回调函数

2.13 completed

  • 回调函数

详见5.3 $.ready()