再来看一下jQuery的扩展方法吧,这可是jQuery的核心。
其实这个方法并不复杂,它的功能有以下几个
1.对object jQuery的扩展,也就是做成所谓的Utilities方法集,方便大家使用jQuery.functionName()的方式调用。
2.对jQuery.fn(也就是jQuery.fn.init.prototype,也就是jQuery.fn.init的实例,也就是调用$()方法返回的对象)的扩展,扩充jQuery实例的功能。
3.对object进行属性或者方法的复制(包括深度复制)。
对jQuery以及jQuery.fn进行扩充,从source里可以看出,因为参数只有一个,走的是个很简单的路径,如果把source抽出来,就如同下面所示:
jQuery.extend = jQuery.fn.extend = function() {
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
// extend jQuery itself if only one argument is passed
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;
}
// Don't bring in undefined values
if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
除去杂乱的变量声明,赋值以及判断。其实就是简单的对对象的属性方法扩充。
如果不是深度复制,那基本上就是对对象方法的clone了,从jQuery的source来看,就是把对象object的方法属性赋值一份。如:5557行(以版本1.4.2的普通版为准)
var opt = jQuery.extend({}, optall)
就是把object optall里的属性方法clone给一个空的object。
剥离出的代码如下:
jQuery.extend = jQuery.fn.extend = function() {
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
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;
}
// Don't bring in undefined values
if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
从source的4931行,还可以看出,这个方法除了clone外,还可以覆盖掉原有object的默认设置。
jQuery.extend( jQuery.ajaxSettings, settings );
深度复制其实就是如果复制对象的属性里有object或者数组的话,使用递归的方式把它们都复制过来。
举个例子来讲
var src = {name:"wqf"};
var copy = {info:{company:"macrohard",position:"CEO"}};
var ret =jQuery.extend(true,src,copy);
得到的结果就是
ret ={name:"wqf",info:{company:"macrohard",position:"CEO"}}
当然,如果出现重复的属性或者方法的话,也会覆盖掉。
这个方法并不复杂,可是却是jQuery里比较重要的一个方法,弄清楚它的几个作用,有助于快速理清一些方法的实现。
分享到:
相关推荐
jquery插件jquery-ui-1.8.18.custom.min.js
${ctx}/plugins/jquery-ui-1.11.2/jquery-ui.css" > ${ctx}/plugins/jquery-ui-1.11.2/external/jquery/jquery.js"> ${ctx}/plugins/jquery-ui-1.11.2/jquery-ui.js"> ${ctx}/plugins/jquery-ui-1.11.2/jquery-ui-...
jquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-uijquery-ui
jquery-ui-1.8.2.custom.min.js,jquery-ui-1.8.4.custom.css
jquery-ui-日期框扩展成时间框 jquery-ui时间框 基于别人的代码进行修改 jquery-ui-1.8.16.custom.css文件末尾加入以下代码 .ui-timepicker-div .ui-widget-header{ margin-bottom: 8px; } .ui-timepicker-div dl{ ...
这里包含jquery-ui-1.8.16.custom.min.js/jquery-ui-1.8.16.custom.css,我费了好长时间找到的,此东西可以配合jQuery UI开发指南一书使用。
jquery插件jquery-ui-1.8.2.custom.min.js
<script src="./public/js/jquery-ui-1.10.3.min.js"> <script src="./public/js/jquery.datepicker-zh-CN.js"></script> <link href="./public/css/jqueryui/jquery-ui-1.10.3.min.css" rel="stylesheet"> $( "#...
jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-ui-1.8.16jquery-...
jquery-ui-1.8.18.custom
jquery-easyui-EDT-1.5.2-build1 jquery-easyui-EDT-1.5.2-build1
压缩包内包含jquery-1.6.4.js jquery-1.6.4.min.js jquery-1.6.4-vsdoc.js 。 【推荐用法】 1、将jquery-1.6.4-vsdoc.js与jquery-1.6.4.js放在同一目录,然后在vs中添加对jquery-1.6.4.js的引用即可; 2、切记:...
jquery-ui-1.7.3.custom JQUERY UI
jquery.mobile-1.4.5 jquery.mobile-1.4.5 jquery.mobile-1.4.5
jQuery版本迁移辅助插件,如果您使用的低版本jQuery改为高版本后出现错误,可以试试这个插件。用来检测和恢复在jQuery1.9版本中已删除或已过时的API。jquery-migrate-1.2.1.js,jquery-migrate-1.2.1.min.js
jquery-ui-1.10.3.js 版本 1积分下载 jquery-ui-1.10.3.js 版本 1积分下载 jquery-ui-1.10.3.js 版本 1积分下载
jquery-ui-1.7.3.custom 完整开发包
jquery-1.4.2-vsdoc.js jquery-1.4.4-vsdoc.js
jquery-ui-1.9.2.custom.min.js
struts2-jquery-plugin-3.1.0.jar