為JavaScript類型增加方法的實(shí)現(xiàn)代碼(增加功能)
更新時(shí)間:2011年12月29日 00:36:59 作者:
大家在js開發(fā)過(guò)程中有些功能已經(jīng)滿足不了我們的需求,或沒(méi)有我們需要的功能,那么我們就可以自己擴(kuò)展下,個(gè)性化js
javaScript的類型函數(shù)(如Number/String/Boolean/Array/Date/Obejct等)都是繼承于 Function.prototype,所以給Function.prototype增加方法,同時(shí)也會(huì)影響到由它衍生的下層類型函數(shù)。如:
Function.prototype.addMethod=function(methodName,func){
if(!this[methodName]){
this[methodName]=func;//給類型增加方法,類似于類型的靜態(tài)方法。func方法是賦于了類型而非實(shí)例。
}
return this;//this 將綁定到方法的調(diào)用對(duì)象(類型函數(shù)),返回this可以進(jìn)行鏈?zhǔn)秸{(diào)用
}
Array.addMethod('testFun',function(){alert(this)});
//Array.testFun(); //function Array() {[native code]}
Object.addMethod('testFun',function(){alert(this)});
//Object.testFun(); //function Object() {[native code]}
Boolean.addMethod('testFun',function(){alert(this)});
//Boolean.testFun(); //function Boolean() {[native code]}
function CustomObject(name,value){
this.name=name || 'CustomObject';
this.value=value || 0;
this.toString=function(){return '[name:'+this.name+',value:'+this.value+']'}
}
CustomObject.addMethod('testFun',function(){alert(this)});
/* return:
* function CustomObject(name, value) {
this.name = name || "CustomObject";
this.value = value || 0;
this.toString = function () {return "[name:" + this.name + ",value:" + this.value + "]";};
}
*/
CustomObject.testFun();
此時(shí)如果用實(shí)例來(lái)調(diào)用的話,則會(huì)報(bào)錯(cuò)。如:
var customObject=new CustomObject(); //定義一個(gè)CustomObject實(shí)例
customObject.testFun();//Error: temp.testFun is not a function
給實(shí)例增加方法
如果給類型實(shí)例增加方法,則應(yīng)該把方法綁定到類型的prototype上。如
Function.prototype.addMethod=function(methodName,func){
if(!this.prototype[methodName]){
this.prototype[methodName]=func;//給原型增加方法,此方法會(huì)影響到該類型的實(shí)例上
}
return this.prototype;//返回原型,此類型實(shí)例可以進(jìn)行鏈形調(diào)用
}
Object.addMethod('testFun',function(){alert(this)});
//({toString:function(){return '[Empty Object]'}}).testFun(); //[Empty Object]
Number.addMethod('testFun',function(){alert(this)});
//(5).testFun(); //5
String.addMethod('testFun',function(){alert(this)});
//'test'.testFun(); //'test'
Boolean.addMethod('testFun',function(){alert(this)});
//true.testFun(); //true
Array.addMethod('testFun',function(){alert(this)});
//(['a','b']).testFun(); //a,b
Date.addMethod('testFun',function(){alert(this)});
//new Date().testFun(); //Tue Dec 27 2011 11:20:58 GMT-0800 (Pacific Standard Time)
function CustomObject(name,value){
this.name=name || 'CustomObject';
this.value=value || 0;
this.toString=function(){return '[name:'+this.name+',value:'+this.value+']'}
}
CustomObject.addMethod('testFun',function(){alert(this)});
var customObject=new CustomObject();
customObject.testFun(); //[name:CustomObject,value:0]
若此時(shí)用類型調(diào)用testFun,則會(huì)報(bào)錯(cuò)。如
Array.addMethod('testFun',function(){alert(this)});
//Array.testFun(); //Error: Array.testFun is not a function
CustomObject.addMethod('testFun',function(){alert(this)});
CustomObject.testFun(); //Error: CustomObject.testFun is not a function
復(fù)制代碼 代碼如下:
Function.prototype.addMethod=function(methodName,func){
if(!this[methodName]){
this[methodName]=func;//給類型增加方法,類似于類型的靜態(tài)方法。func方法是賦于了類型而非實(shí)例。
}
return this;//this 將綁定到方法的調(diào)用對(duì)象(類型函數(shù)),返回this可以進(jìn)行鏈?zhǔn)秸{(diào)用
}
Array.addMethod('testFun',function(){alert(this)});
//Array.testFun(); //function Array() {[native code]}
Object.addMethod('testFun',function(){alert(this)});
//Object.testFun(); //function Object() {[native code]}
Boolean.addMethod('testFun',function(){alert(this)});
//Boolean.testFun(); //function Boolean() {[native code]}
function CustomObject(name,value){
this.name=name || 'CustomObject';
this.value=value || 0;
this.toString=function(){return '[name:'+this.name+',value:'+this.value+']'}
}
CustomObject.addMethod('testFun',function(){alert(this)});
/* return:
* function CustomObject(name, value) {
this.name = name || "CustomObject";
this.value = value || 0;
this.toString = function () {return "[name:" + this.name + ",value:" + this.value + "]";};
}
*/
CustomObject.testFun();
此時(shí)如果用實(shí)例來(lái)調(diào)用的話,則會(huì)報(bào)錯(cuò)。如:
復(fù)制代碼 代碼如下:
var customObject=new CustomObject(); //定義一個(gè)CustomObject實(shí)例
customObject.testFun();//Error: temp.testFun is not a function
給實(shí)例增加方法
如果給類型實(shí)例增加方法,則應(yīng)該把方法綁定到類型的prototype上。如
復(fù)制代碼 代碼如下:
Function.prototype.addMethod=function(methodName,func){
if(!this.prototype[methodName]){
this.prototype[methodName]=func;//給原型增加方法,此方法會(huì)影響到該類型的實(shí)例上
}
return this.prototype;//返回原型,此類型實(shí)例可以進(jìn)行鏈形調(diào)用
}
Object.addMethod('testFun',function(){alert(this)});
//({toString:function(){return '[Empty Object]'}}).testFun(); //[Empty Object]
Number.addMethod('testFun',function(){alert(this)});
//(5).testFun(); //5
String.addMethod('testFun',function(){alert(this)});
//'test'.testFun(); //'test'
Boolean.addMethod('testFun',function(){alert(this)});
//true.testFun(); //true
Array.addMethod('testFun',function(){alert(this)});
//(['a','b']).testFun(); //a,b
Date.addMethod('testFun',function(){alert(this)});
//new Date().testFun(); //Tue Dec 27 2011 11:20:58 GMT-0800 (Pacific Standard Time)
function CustomObject(name,value){
this.name=name || 'CustomObject';
this.value=value || 0;
this.toString=function(){return '[name:'+this.name+',value:'+this.value+']'}
}
CustomObject.addMethod('testFun',function(){alert(this)});
var customObject=new CustomObject();
customObject.testFun(); //[name:CustomObject,value:0]
若此時(shí)用類型調(diào)用testFun,則會(huì)報(bào)錯(cuò)。如
復(fù)制代碼 代碼如下:
Array.addMethod('testFun',function(){alert(this)});
//Array.testFun(); //Error: Array.testFun is not a function
CustomObject.addMethod('testFun',function(){alert(this)});
CustomObject.testFun(); //Error: CustomObject.testFun is not a function
相關(guān)文章
識(shí)別操作系統(tǒng)是不是vista的js代碼
識(shí)別操作系統(tǒng)是不是vista的js代碼...2007-08-08js實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03director.js實(shí)現(xiàn)前端路由使用實(shí)例
這篇文章主要介紹了director.js實(shí)現(xiàn)前端路由使用實(shí)例,director.js是最純粹的路由注冊(cè)/解析器,它在不刷新頁(yè)面的情況下,利用“#”符號(hào)組織不同的URL路徑,需要的朋友可以參考下2015-02-02TypeScript實(shí)現(xiàn)字符串轉(zhuǎn)樹結(jié)構(gòu)的方法詳解
有一個(gè)多行字符串,每行開頭會(huì)用空格來(lái)表示它的層級(jí)關(guān)系,每間隔一層它的空格總數(shù)為2,如何將它轉(zhuǎn)為json格式的樹型數(shù)據(jù)?本文就跟大家分享下這個(gè)算法2022-09-09extjs 04_grid 單擊事件新發(fā)現(xiàn)
EXTJS GRID 中單擊行和單元格獲得行或者單元格的內(nèi)容(數(shù)據(jù)),本文將整理此功能的應(yīng)用,需要了解的朋友可以參考下2012-11-11javascript阻止scroll事件多次執(zhí)行的思路及實(shí)現(xiàn)
阻止scroll事件多次執(zhí)行主要是為了解決一些常見網(wǎng)頁(yè)特效在js解析時(shí)預(yù)期與效果不同,感興趣的朋友可以了解下2013-11-11JS使用canvas中的measureText方法測(cè)量字體寬度示例
這篇文章主要介紹了JS使用canvas中的measureText方法測(cè)量字體寬度,結(jié)合實(shí)例形式分析了canvas的measureText方法相關(guān)使用技巧,需要的朋友可以參考下2019-02-02