為JavaScript類型增加方法的實現(xiàn)代碼(增加功能)
更新時間:2011年12月29日 00:36:59 作者:
大家在js開發(fā)過程中有些功能已經(jīng)滿足不了我們的需求,或沒有我們需要的功能,那么我們就可以自己擴(kuò)展下,個性化js
javaScript的類型函數(shù)(如Number/String/Boolean/Array/Date/Obejct等)都是繼承于 Function.prototype,所以給Function.prototype增加方法,同時也會影響到由它衍生的下層類型函數(shù)。如:
Function.prototype.addMethod=function(methodName,func){
if(!this[methodName]){
this[methodName]=func;//給類型增加方法,類似于類型的靜態(tài)方法。func方法是賦于了類型而非實例。
}
return this;//this 將綁定到方法的調(diào)用對象(類型函數(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();
此時如果用實例來調(diào)用的話,則會報錯。如:
var customObject=new CustomObject(); //定義一個CustomObject實例
customObject.testFun();//Error: temp.testFun is not a function
給實例增加方法
如果給類型實例增加方法,則應(yīng)該把方法綁定到類型的prototype上。如
Function.prototype.addMethod=function(methodName,func){
if(!this.prototype[methodName]){
this.prototype[methodName]=func;//給原型增加方法,此方法會影響到該類型的實例上
}
return this.prototype;//返回原型,此類型實例可以進(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]
若此時用類型調(diào)用testFun,則會報錯。如
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方法是賦于了類型而非實例。
}
return this;//this 將綁定到方法的調(diào)用對象(類型函數(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();
此時如果用實例來調(diào)用的話,則會報錯。如:
復(fù)制代碼 代碼如下:
var customObject=new CustomObject(); //定義一個CustomObject實例
customObject.testFun();//Error: temp.testFun is not a function
給實例增加方法
如果給類型實例增加方法,則應(yīng)該把方法綁定到類型的prototype上。如
復(fù)制代碼 代碼如下:
Function.prototype.addMethod=function(methodName,func){
if(!this.prototype[methodName]){
this.prototype[methodName]=func;//給原型增加方法,此方法會影響到該類型的實例上
}
return this.prototype;//返回原型,此類型實例可以進(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]
若此時用類型調(diào)用testFun,則會報錯。如
復(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)文章
TypeScript實現(xiàn)字符串轉(zhuǎn)樹結(jié)構(gòu)的方法詳解
有一個多行字符串,每行開頭會用空格來表示它的層級關(guān)系,每間隔一層它的空格總數(shù)為2,如何將它轉(zhuǎn)為json格式的樹型數(shù)據(jù)?本文就跟大家分享下這個算法2022-09-09extjs 04_grid 單擊事件新發(fā)現(xiàn)
EXTJS GRID 中單擊行和單元格獲得行或者單元格的內(nèi)容(數(shù)據(jù)),本文將整理此功能的應(yīng)用,需要了解的朋友可以參考下2012-11-11javascript阻止scroll事件多次執(zhí)行的思路及實現(xiàn)
阻止scroll事件多次執(zhí)行主要是為了解決一些常見網(wǎng)頁特效在js解析時預(yù)期與效果不同,感興趣的朋友可以了解下2013-11-11JS使用canvas中的measureText方法測量字體寬度示例
這篇文章主要介紹了JS使用canvas中的measureText方法測量字體寬度,結(jié)合實例形式分析了canvas的measureText方法相關(guān)使用技巧,需要的朋友可以參考下2019-02-02