模仿JQuery.extend函數(shù)擴(kuò)展自己對(duì)象的js代碼
更新時(shí)間:2009年12月09日 22:48:54 作者:
最近打算寫個(gè)自己的js工具集合,把自己平常經(jīng)常使用的方法很好的封裝起來(lái),其中模仿了jq的結(jié)構(gòu)。
但在寫的過(guò)程中發(fā)現(xiàn),如果要在之前寫好的對(duì)象中添加新的靜態(tài)方法或?qū)嵗椒ǎ薷脑械膶?duì)象結(jié)構(gòu),于是查看了jquery了extend方法,果然extend方法支持了jq的半邊天,拿來(lái)主義,給自己的對(duì)象做擴(kuò)張用。
下面進(jìn)入正題:
假如有以下一個(gè)對(duì)象
var MyMath = {
//加法
Add: function(a, b){
return a + b;
},
//減法
Sub: function(a, b){
return a - b;
}
}
對(duì)象名MyMath,有兩個(gè)靜態(tài)方法Add和Sub,正常調(diào)用:
alert(MyMath.Add(3, 5)) //結(jié)果8
好,現(xiàn)在如果現(xiàn)在MyMath增加兩個(gè)靜態(tài)方法(乘法、除法)怎么辦,并且不要修改之前寫好的對(duì)象,以前我們可以這么做:
//新加一靜態(tài)方法:Mul乘法
MyMath["Mul"] = function(a, b){
return a * b;
}
//新加一靜態(tài)方法:Div除法
MyMath["Div"] = function(a, b){
return a / b;
}
這樣,我們給MyMath添加兩個(gè)方法:Mul和Div。正常調(diào)用:
alert(MyMath.Add(3, 5)) //結(jié)果8
alert(MyMath.Mul(3, 5)) //結(jié)果15
但是,剛才增加方法的寫法有點(diǎn)笨拙,每增加一個(gè)方法都要寫一次對(duì)象名(MyMath),能不能想之前我們創(chuàng)建對(duì)象的時(shí)候那樣,通過(guò)Json的結(jié)構(gòu)去聲明一個(gè)對(duì)象呢?
答案當(dāng)然是可以了,通過(guò)模擬JQuery.extend函數(shù),輕松做到。以下提取JQuery.extend函數(shù)并修改了函數(shù)名:
MyMath.extend = function(){
// copy reference to target object
var target = arguments[0] ||
{}, i = 1, length = arguments.length, deep = false, options;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] ||
{};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !jQuery.isFunction(target))
target = {};
// 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 (var name in options) {
var src = target[name], copy = options[name];
// Prevent never-ending loop
if (target === copy)
continue;
// Recurse if we're merging object values
if (deep && copy && typeof copy === "object" && !copy.nodeType)
target[name] = jQuery.extend(deep, // Never move original objects, clone them
src || (copy.length != null ? [] : {}), copy);
// Don't bring in undefined values
else
if (copy !== undefined)
target[name] = copy;
}
// Return the modified object
return target;
};
現(xiàn)在我們通過(guò)這個(gè)extend方法來(lái)增加剛才我們的方法(乘法、除法):
MyMath.extend({
Mul: function(a, b){
return a * b;
},
Div: function(a, b){
return a / b;
}
});
這樣的結(jié)構(gòu)更加一目了然。
轉(zhuǎn)載請(qǐng)注上來(lái)自:http://www.cnblogs.com/wbkt2t
下面進(jìn)入正題:
假如有以下一個(gè)對(duì)象
復(fù)制代碼 代碼如下:
var MyMath = {
//加法
Add: function(a, b){
return a + b;
},
//減法
Sub: function(a, b){
return a - b;
}
}
對(duì)象名MyMath,有兩個(gè)靜態(tài)方法Add和Sub,正常調(diào)用:
復(fù)制代碼 代碼如下:
alert(MyMath.Add(3, 5)) //結(jié)果8
好,現(xiàn)在如果現(xiàn)在MyMath增加兩個(gè)靜態(tài)方法(乘法、除法)怎么辦,并且不要修改之前寫好的對(duì)象,以前我們可以這么做:
復(fù)制代碼 代碼如下:
//新加一靜態(tài)方法:Mul乘法
MyMath["Mul"] = function(a, b){
return a * b;
}
//新加一靜態(tài)方法:Div除法
MyMath["Div"] = function(a, b){
return a / b;
}
這樣,我們給MyMath添加兩個(gè)方法:Mul和Div。正常調(diào)用:
復(fù)制代碼 代碼如下:
alert(MyMath.Add(3, 5)) //結(jié)果8
alert(MyMath.Mul(3, 5)) //結(jié)果15
但是,剛才增加方法的寫法有點(diǎn)笨拙,每增加一個(gè)方法都要寫一次對(duì)象名(MyMath),能不能想之前我們創(chuàng)建對(duì)象的時(shí)候那樣,通過(guò)Json的結(jié)構(gòu)去聲明一個(gè)對(duì)象呢?
答案當(dāng)然是可以了,通過(guò)模擬JQuery.extend函數(shù),輕松做到。以下提取JQuery.extend函數(shù)并修改了函數(shù)名:
復(fù)制代碼 代碼如下:
MyMath.extend = function(){
// copy reference to target object
var target = arguments[0] ||
{}, i = 1, length = arguments.length, deep = false, options;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] ||
{};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !jQuery.isFunction(target))
target = {};
// 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 (var name in options) {
var src = target[name], copy = options[name];
// Prevent never-ending loop
if (target === copy)
continue;
// Recurse if we're merging object values
if (deep && copy && typeof copy === "object" && !copy.nodeType)
target[name] = jQuery.extend(deep, // Never move original objects, clone them
src || (copy.length != null ? [] : {}), copy);
// Don't bring in undefined values
else
if (copy !== undefined)
target[name] = copy;
}
// Return the modified object
return target;
};
現(xiàn)在我們通過(guò)這個(gè)extend方法來(lái)增加剛才我們的方法(乘法、除法):
復(fù)制代碼 代碼如下:
MyMath.extend({
Mul: function(a, b){
return a * b;
},
Div: function(a, b){
return a / b;
}
});
這樣的結(jié)構(gòu)更加一目了然。
轉(zhuǎn)載請(qǐng)注上來(lái)自:http://www.cnblogs.com/wbkt2t
您可能感興趣的文章:
- 原生js實(shí)現(xiàn)復(fù)制對(duì)象、擴(kuò)展對(duì)象 類似jquery中的extend()方法
- 淺談jQuery中的$.extend方法來(lái)擴(kuò)展JSON對(duì)象
- 基于jQuery的一個(gè)擴(kuò)展form序列化到j(luò)son對(duì)象
- 擴(kuò)展jQuery對(duì)象時(shí)如何擴(kuò)展成員變量具體怎么實(shí)現(xiàn)
- JQuery 動(dòng)態(tài)擴(kuò)展對(duì)象之另類視角
- jQuery.extend()、jQuery.fn.extend()擴(kuò)展方法示例詳解
- Jquery 的擴(kuò)展方法總結(jié)
- Jquery中擴(kuò)展方法extend使用技巧
- 修改或擴(kuò)展jQuery原生方法的代碼實(shí)例
- JavaScript自執(zhí)行函數(shù)和jQuery擴(kuò)展方法詳解
- JQuery擴(kuò)展對(duì)象方法操作示例
相關(guān)文章
微信小程序利用for循環(huán)解決內(nèi)容變更問(wèn)題
這篇文章主要介紹了微信小程序利用for循環(huán)解決內(nèi)容變更問(wèn)題 ,本文分步驟通過(guò)實(shí)例代碼詳解給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
微信小程序頁(yè)面滾動(dòng)到指定位置代碼實(shí)例
這篇文章主要介紹了微信小程序頁(yè)面滾動(dòng)到指定位置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
javascript實(shí)現(xiàn)網(wǎng)站加入收藏功能
這篇文章主要介紹了javascript實(shí)現(xiàn)網(wǎng)站加入收藏功能的相關(guān)資料,需要的朋友可以參考下2015-12-12
BootStrap 動(dòng)態(tài)添加驗(yàn)證項(xiàng)和取消驗(yàn)證項(xiàng)的實(shí)現(xiàn)方法
這篇文章主要介紹了BootStrap 動(dòng)態(tài)添加驗(yàn)證項(xiàng)和取消驗(yàn)證項(xiàng)的實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
JS 實(shí)現(xiàn) ajax 異步瀏覽器兼容問(wèn)題
本文通過(guò)實(shí)例代碼給大家講解了js實(shí)現(xiàn)ajax異步瀏覽器兼容問(wèn)題,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01
實(shí)例講解javascript實(shí)現(xiàn)異步圖片上傳方法
給大家詳細(xì)講解一下如何通過(guò)javascript寫出異步圖片上傳,并且把實(shí)例代碼給大家分享了下,有興趣的讀者們測(cè)試一下吧。2017-12-12

