為JavaScript類型增加方法的實(shí)現(xiàn)代碼(增加功能)
更新時(shí)間:2011年12月29日 00:36:59 作者:
大家在js開發(fā)過程中有些功能已經(jīng)滿足不了我們的需求,或沒有我們需要的功能,那么我們就可以自己擴(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í)例來調(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í)例來調(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)文章
基于canvas實(shí)現(xiàn)的鐘擺效果完整實(shí)例
這篇文章主要介紹了基于canvas實(shí)現(xiàn)的鐘擺效果,以完整實(shí)例形式分析了JavaScript結(jié)合html5的canvas技術(shù)實(shí)現(xiàn)鐘擺動(dòng)態(tài)旋轉(zhuǎn)效果的方法,需要的朋友可以參考下2016-01-01
基于javascript的無縫滾動(dòng)動(dòng)畫實(shí)現(xiàn)2
這篇文章主要介紹了基于javascript的無縫滾動(dòng)動(dòng)畫實(shí)現(xiàn)2,文章通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
canvas實(shí)現(xiàn)弧形可拖動(dòng)進(jìn)度條效果
本篇文章主要介紹了canvas實(shí)現(xiàn)弧形可拖動(dòng)進(jìn)度條的實(shí)例方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05
HTML+CSS+JavaScript做女朋友版的刮刮樂(一看就會(huì))
這篇文章主要介紹了HTML+CSS+JavaScript做女朋友版的刮刮樂(一看就會(huì))本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
jqgrid 表格數(shù)據(jù)導(dǎo)出實(shí)例
jqgrid并沒有自帶導(dǎo)出表格數(shù)據(jù)的方法,這里就自己實(shí)現(xiàn)了一個(gè),嘗試過在頁面直接將數(shù)據(jù)導(dǎo)出,發(fā)現(xiàn)只有IE下可以通過調(diào)用saveas來實(shí)現(xiàn),但是別的瀏覽器不支持,于是考慮將數(shù)據(jù)傳回后臺(tái),然后后臺(tái)返回下載文件來實(shí)現(xiàn)2013-11-11
僅30行代碼實(shí)現(xiàn)Javascript中的MVC
這篇文章主要介紹了僅30行代碼實(shí)現(xiàn)Javascript中的MVC的方法,MVC的基礎(chǔ)是觀察者模式,這是實(shí)現(xiàn)model和view同步的關(guān)鍵,想要深入了解的朋友可以參考本文2016-02-02

