jQuery源代码学习笔记:构造jQuery对象
2.1源代码结构:
(function( window, undefined ) { var jQuery = (function() {
// 构建jQuery对象
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
} // jQuery对象原型
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// selector有下面7种分支情况:
// DOM元素
// body(优化)
// 字符串:HTML标签、HTML字符串、#id、选择器表达式
// 函数(作为ready回调函数)
// 最后返回伪数组
}
}; // Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn; // 合并内容到第一个參数中,兴许大部分功能都通过该函数扩展
// 通过jQuery.fn.extend扩展的函数,大部分都会调用通过jQuery.extend扩展的同名函数
jQuery.extend = jQuery.fn.extend = function() {}; // 在jQuery上扩展静态方法
jQuery.extend({
// ready bindReady
// isPlainObject isEmptyObject
// parseJSON parseXML
// globalEval
// each makeArray inArray merge grep map
// proxy
// access
// uaMatch
// sub
// browser
}); // 到这里,jQuery对象构造完毕。后边的代码都是对jQuery或jQuery对象的扩展
return jQuery; })(); window.jQuery = window.$ = jQuery;
})(window);
1、jQuery()返回的jQuery对象实际上是构造函数jQuery.fn.init()的实例,可是为什么能在构造函数jQuery.fn.init()的实例上调用构造函数jQuery()的原型方法和属性?如$("#id").length和$("#id").size()
jQuery.fn.init.prototype = jQuery.fn,用构造函数的原型对象覆盖了jQuery.fn.init()的原型对象
2、为什么要覆盖构造函数jQuery()的原型对象jQuery.prototype?
在jQury.prototype上定义的属性和方法会被全部jQuery对象继承,这样能够有效降低每一个jQuery对象所需的内存。
学以致用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
!(function (window, undefined) {
var
rootjQuery,
jQuery = (function () {
var jQuery = function (selector, context, rootjQuery) {
return new jQuery.fn.init(selector, context);
};
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function (selector, context, rootjQuery) {
//alert("test")
},
length: 0,
size: function(){
return this.length;
}
};
jQuery.fn.init.prototype = jQuery.fn;
return jQuery;
})(); window.jQuery = window.$ = jQuery;
})(window); </script>
<script type="text/javascript">
alert($("").size());
</script>
</head>
<body> </body>
</html>
2.2 jQuery.extend = jQuery.fn.extend
// 合并两个或很多其它对象的属性到第一个对象中,jQuery兴许的大部分功能都通过该函数扩展
// 通过jQuery.fn.extend扩展的函数,大部分都会调用通过jQuery.extend扩展的同名函数 // 假设传入两个或多个对象。全部对象的属性会被加入到第一个对象target // 假设仅仅传入一个对象,则将对象的属性加入到jQuery对象中。
// 用这样的方式,我们能够为jQuery命名空间添加新的方法。 能够用于编写jQuery插件。
// 假设不想改变传入的对象,能够传入一个空对象:$.extend({}, object1, object2);
// 默认合并操作是不迭代的。即便target的某个属性是对象或属性。也会被全然覆盖而不是合并
// 第一个參数是true,则会迭代合并
// 从object原型继承的属性会被拷贝
// undefined值不会被拷贝
// 由于性能原因,JavaScript自带类型的属性不会合并 // jQuery.extend( target, [ object1 ], [ objectN ] )
// jQuery.extend( [ deep ], target, object1, [ objectN ] )
jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false; // Handle a deep copy situation
// 假设第一个參数是boolean型,可能是深度拷贝
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
// 跳过boolean和target,从第3个開始
i = 2;
} // Handle case when target is a string or something (possible in deep copy)
// target不是对象也不是函数。则强制设置为空对象
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
} // extend jQuery itself if only one argument is passed
// 假设仅仅传入一个參数。则觉得是对jQuery扩展
if ( length === i ) {
target = this;
--i;
} for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
// 仅仅处理非空參数
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ]; // Prevent never-ending loop
// 避免循环引用
if ( target === copy ) {
continue;
} // Recurse if we're merging plain objects or arrays
// 深度拷贝且值是纯对象或数组,则递归
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
// 假设copy是数组
if ( copyIsArray ) {
copyIsArray = false;
// clone为src的修正值
clone = src && jQuery.isArray(src) ? src : [];
// 假设copy的是对象
} else {
// clone为src的修正值
clone = src && jQuery.isPlainObject(src) ? src : {};
} // Never move original objects, clone them
// 递归调用jQuery.extend
target[ name ] = jQuery.extend( deep, clone, copy ); // Don't bring in undefined values
// 不能拷贝空值
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
} // Return the modified object
// 返回更改后的对象
return target;
};
jQuery源代码学习笔记:构造jQuery对象的更多相关文章
- jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释
3.1 源代码 init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(&qu ...
- jQuery源代码学习笔记_工具函数_noop/error/now/trim
jQuery源代码学习笔记_工具函数_noop/error/now/trim jquery提供了一系列的工具函数,用于支持其运行,今天主要分析noop/error/now/trim这4个函数: 1.n ...
- jQuery源代码学习笔记_01
如何获取jQuery源代码 1.可以从GitHub上下载到没有合并和压缩的源代码 2.如果要查看兼容IE6-8的版本,请选择1.x-master分支 3.可以使用git clone也可以使用downl ...
- jQuery源代码学习之九—jQuery事件模块
jQuery事件系统并没有将事件坚挺函数直接绑定在DOM元素上,而是基于事件缓存模块来管理监听函数的. 二.jQuery事件模块的代码结构 //定义了一些正则 // // //jQuery事件对象 j ...
- jQuery源代码学习笔记_bind
一般想到JS的兼容性问题的时候,首先会想到addEventListener与attachEvent这一对冤家,那么我们先来看看它们有什么兼容性问题 addEventListener与attachEve ...
- jQuery 学习笔记:jQuery 代码结构
jQuery 学习笔记:jQuery 代码结构 这是我学习 jQuery 过程中整理的笔记,这一部分主要包括 jQuery 的代码最外层的结构,写出来整理自己的学习成果,有错误欢迎指出. jQuery ...
- 【学习笔记】jQuery的基础学习
[学习笔记]jQuery的基础学习 新建 模板 小书匠 什么是jQuery对象? jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQuery 独有的. 如果 ...
- jQuery源代码学习_工具函数_type
jquery源代码学习_工具函数_type jquery里面有一个很重要的工具函数,$.type函数用来判断类型,今天写这篇文章,是来回顾type函数的设计思想,深入理解. 首先来看一下最终结果: 上 ...
- jQuery学习笔记之jQuery的Ajax(3)
jQuery学习笔记之jQuery的Ajax(3) 6.jQuery的Ajax插件 源码地址: https://github.com/iyun/jQueryDemo.git ------------- ...
随机推荐
- cocos-html5 JS 写法基础 语言核心
转载:http://blog.csdn.net/leasystu/article/details/18735797 cocos2dx 3.0 js继承:John Resiq的继承写法解析 CCClas ...
- Linux内存初始化
start_kernel -> setup_arch 在这个函数中我们主要看这几个函数. machine_specific_memory_setup max_low_pfn = setup_me ...
- 【HDOJ】2065 "红色病毒"问题
刚开始看这道题目的时候,完全没看出来是递推.看了网上大牛的分析.立刻就明白了.其实无论字符串长度为多少,都可以将该长度下的组合分成四种情况S1(A偶数C偶数).S2(A偶数C奇数).S3(A奇数C偶数 ...
- javascript--苹果系统底部菜单--详细分析(转)
源码下载:http://pan.baidu.com/s/1hqvJJA8 代码来源: 这个DEMO来自“妙味课堂” 昨天看到了“妙味课堂”的一个苹果菜单的DEMO.根据里面提到的“勾股定理”.我自己分 ...
- c#打印机设置,取得打印机列表及相应打印机的所有纸张格式
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- IIS7配置https
To Install an SSL Certificate in Microsoft IIS 7 Click Start, mouse-over Administrative Tools, and t ...
- POJ 3159 Candies 差分约束dij
分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...
- SQL SERVER 2008查询其他数据库
1.访问本地的其他数据库 --启用Ad Hoc Distributed Queries-- reconfigure reconfigure -- 使用完成后,关闭Ad Hoc Distributed ...
- while MyJob = '程序员' do --- 序
因为自己的际遇,忽然想写点什么留在这个世上.也许只是想证明自己活过吧. 所以,这不会是一个过去时的小说,这将是一个接近进行时的记叙.之所以是接近,因为我只有在空余时间,才能记录最近的经历.出于保护隐私 ...
- Java笔记(十七)……异常
异常概述 异常是什么 是对问题的描述,将问题进行对象的封装 异常体系 Throwable |---Error |---Exception |---RuntimeException 异常体系的特点 异常 ...