本文将分析(21 , 94) 定义了一些变量和函数 jquery = function(){};
分析的具体部分的源码(31-64) 【因为包含注释可能行号有所误差 (>^ω^<)
// 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 );
},
// 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.$,
//以上和防冲突有关 例:
var $ = 10 ;
<script src="../jQuery2.0.3.js"></script>
$();
这时jQuery中的_$就变成了10 如果外部没有定义 则为undefined ,等后文提到防冲突的时候细说=、=
// [[Class]] -> type pairs
class2type = {}, //定义了一个对象字面量,在类型判断中用到即$.type() 最终的存储效果如下
class2type = { '[Object String]' : string , '[Object Array]' : 'array' },
// 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 );
},
// 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 );
},
//重中之重!!jQuery.fn是什么? 根据96行jQuery.fn = jQuery.prototype, fn实际就是原形
先说下普通下面向对象的方法
function aaa(){
}
aaa.prototype.init();
aaa.prototype.css();
var a1 = new aaa();
a1.init();
但是jQuery中没有这么做
function jQuery(){
return new jQuery.prototype.init();
}
jQuery.prototype.init();
jQuery.prototype.css();
但是此时返回的对象并不能通过类似jQuery().css()这种调用方法! 那jQuery是怎么找到的呢?
见283行 jQuery.fn.init.prototype = jQuery.fn;
实际上就是把jQuery的原型赋给了init()返回的对象的原型! 这样就可以直接调用fn下面的css()方法了!