亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery中extend函數(shù)詳解

 更新時(shí)間:2015年02月13日 16:14:37   投稿:hebedich  
這篇文章主要介紹了jQuery中extend函數(shù)詳解,十分詳細(xì),需要的朋友可以參考下

在jQuery的API手冊(cè)中,我們看到,extend實(shí)際上是掛載在jQuery和jQuery.fn上的兩個(gè)不同方法,盡管在jQuery內(nèi)部jQuery.extend()和jQuery.fn.extend()是用相同的代碼實(shí)現(xiàn)的,但是它們的功能卻不太一樣。來看一下 官方API對(duì)extend 的解釋:

代碼如下:

復(fù)制代碼 代碼如下:

jQuery.extend(): Merge the contents of two or more objects together into the first object.(把兩個(gè)或者更多的對(duì)象合并到第一個(gè)當(dāng)中)
jQuery.fn.extend():Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(把對(duì)象掛載到j(luò)Query的prototype屬性,來擴(kuò)展一個(gè)新的jQuery實(shí)例方法)

我們知道,jQuery有靜態(tài)方法和實(shí)例方法之分, 那么jQuery.extend()和jQuery.fn.extend()的第一個(gè)區(qū)別就是一個(gè)用來擴(kuò)展靜態(tài)方法,一個(gè)用來擴(kuò)展實(shí)例方法。用法如下:

復(fù)制代碼 代碼如下:

jQuery.extend({
sayhello: function (){
console.log( "Hello,This is jQuery Library" );
}
})
$.sayhello(); //Hello, This is jQuery Library
jQuery.fn.extend({
check: function () {
return this .each( function () {
this .checked = true ;
});
},
uncheck: function () {
return this .each( function () {
this .checked = false ;
});
}
})
$( "input[type='checkbox']" ).check(); //所有的checkbox都會(huì)被選擇

注意兩種調(diào)用插件的方式,一種是直接用$調(diào)用,另外一種是用$()調(diào)用,另外jQuery.extend()接收多個(gè)對(duì)象作為參數(shù),如果只有一個(gè)參數(shù),則把這個(gè)對(duì)象的屬性方法附加到j(luò)Query上,如果含有多個(gè)參數(shù),則把后面的對(duì)象的屬性和方法附加到第一個(gè)對(duì)象上。jQuery extend的實(shí)現(xiàn)源碼:

復(fù)制代碼 代碼如下:

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
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
if ( length === i ) {
target = this ;
--i;
}
for ( ; i < length; i++ ) {
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 ;
}
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false ;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
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;
};

很大一堆代碼,乍看起來難以理解,其實(shí)代碼的大部分都是用來實(shí)現(xiàn)jQuery.extend()中有多個(gè)參數(shù)時(shí)的對(duì)象合并,深度拷貝問題,如果去掉這些功能,讓extend只有擴(kuò)展靜態(tài)和實(shí)例方法的功能,那么代碼如下:

復(fù)制代碼 代碼如下:

jQuery.extend = jQuery.fn.extend = function (obj){
//obj是傳遞過來擴(kuò)展到this上的對(duì)象
var target= this ;
for ( var name in obj){
//name為對(duì)象屬性
//copy為屬性值
copy=obj[name];
//防止循環(huán)調(diào)用
if (target === copy) continue ;
//防止附加未定義值
if ( typeof copy === 'undefined' ) continue ;
//賦值
target[name]=copy;
}
return target;
}

下面再來對(duì)extend方法進(jìn)行注釋解釋:

復(fù)制代碼 代碼如下:

jQuery.extend = jQuery.fn.extend = function () {
// 定義默認(rèn)參數(shù)和變量
// 對(duì)象分為擴(kuò)展對(duì)象和被擴(kuò)展的對(duì)象
//options 代表擴(kuò)展的對(duì)象中的方法
//name 代表擴(kuò)展對(duì)象的方法名
//i 為擴(kuò)展對(duì)象參數(shù)起始值
//deep 默認(rèn)為淺復(fù)制
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false ;
//對(duì)接下來的參數(shù)進(jìn)行處理
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
i = 2;
}
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
if ( length === i ) {
target = this ;
--i;
}
//對(duì)從i開始的多個(gè)參數(shù)進(jìn)行遍歷
for ( ; i < length; i++ ) {
// 只處理有定義的值
if ( (options = arguments[ i ]) != null ) {
// 展開擴(kuò)展對(duì)象
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// 防止循環(huán)引用
if ( target === copy ) {
continue ;
}
// 遞歸處理深拷貝
if ( deep && copy &&; ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false ;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
target[ name ] = jQuery.extend( deep, clone, copy );
// 不處理未定義值
} else if ( copy !== undefined ) {
//給target增加屬性或方法
target[ name ] = copy;
}
}
}
}
//返回
return target;
};

弄懂了jQuery擴(kuò)展的原理,相信以后再也不用為編寫jQuery插件而煩惱了。

相關(guān)文章

最新評(píng)論