給Function做的OOP擴(kuò)展
更新時間:2009年05月07日 15:07:22 作者:
自編框架Megami的一部分……現(xiàn)在發(fā)現(xiàn)自己實(shí)在太宅了……
文件中出現(xiàn)的名字請對號入座……
復(fù)制代碼 代碼如下:
// 下面是OOP用的方法
// 這樣很猥瑣……因?yàn)镴S并不是OOP語言……
// 但偉大的伍迷指引我們來這么干
// Belldandy會保佑用這些方法來OOP的人的……
Function.prototype.inherits = function(base){
//派生關(guān)系,保留了prototype
//只支持單派生
this.prototype = new base();
return this;
}
Function.prototype.create = function(){
//類的創(chuàng)建器,和用new等價
//JS不支持在構(gòu)造器用call和apply,所以……
//Belldandy啊,感謝你告訴我怎么解決這個問題啊……
var _args = [];
for(i=0;i<arguments.length;i++) _args.push('arguments['+i+']');
return eval('new this('+_args.join(',')+')'); //eval都用上了……Bell啊,下次給個好點(diǎn)的主意吧……
}
Function.prototype.pin = function(pinner,args){
// 注冊服務(wù),或者叫“pin”服務(wù)
// EventManager就可以這么干
// 你也可以認(rèn)為實(shí)現(xiàn)了有默認(rèn)實(shí)現(xiàn)的接口……
// 例如,pin EventManager就可以這樣:Class.pin(core.WvwntManager)
args = args || [];
pinner.apply(this.prototype,args);
return this;
}
Function.prototype.method = function(name, f) { //添加方法,高效
if (!(f instanceof Function)) throw new Error('方法綁定無效,得到類型'+typeof f+';期待為function');
this.prototype[name] = f;
return this
}
Function.prototype.property = function(name, localName, getter, setter) { //添加屬性,可自定getter、setter
if (!name || !name instanceof String) throw new EnvironmentException('定義屬性時,屬性名沒有定義,或者不是字符串');
if (!localName || !localName instanceof String) localName = '_local_' + name;
if(getter instanceof Function) {
this.prototype['_belldandy_get_'+name] = getter;
}
if(setter instanceof Function){
this.prototype['_belldandy_set_'+name] = setter;
}
this.prototype[name] = new Function("value , force"," \
if (!value && !force) { \
if (!this['"+'_belldandy_get_'+name+"'] || !this['"+'_belldandy_get_'+name+"'] instanceof Function) \
return this['"+localName+"']; /* 沒有設(shè)置getter時 */\
else \
return this['"+'_belldandy_get_'+name+"'].call(this); \
} else { \
if (!this['"+'_belldandy_set_'+name+"'] || !this['"+'_belldandy_set_'+name+"'] instanceof Function) \
this['"+localName+"'] = value; \
else\
this['"+'_belldandy_set_'+name+"'].call(this, value); \
return this\
}") //Belldandy啊,饒恕我吧,雖然這樣不產(chǎn)生閉包
return this;
}
Function.prototype.static = function(name,value){ //靜態(tài)特征,包括屬性和方法
this[name] = value;
return this;
}
使用效果如下:
復(fù)制代碼 代碼如下:
function foo() { };
foo
.property('a', '_a')
.property('b', '_b', function() { return this._b + '.' })
.method('f', function() { dwn(this.a()) });
function bar(x,y){this.x = x;this.y = y;};
with(bar){
inherits(foo)
method('g',function(){dwn(this.a()+'-'+this.b())})
}
var f = new foo();
f.a(1);
f.b(2);
dwn(f.a());
dwn(f.b());
f.f();
b = bar.create(1,2);
b.a(4);
b.b(5);
dwn(b.x+','+b.y);
b.g();
//dwn自己參閱月影的書
相關(guān)文章
JavaScript使用URL.canParse驗(yàn)證URL的方法詳解
JavaScript誕生以來,一直沒有一種簡單的方法驗(yàn)證URL,現(xiàn)在JavaScript新增了一個新方法——URL.canParse,文中通過代碼示例和圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
JS實(shí)現(xiàn)向iframe中表單傳值的方法
這篇文章主要介紹了JS實(shí)現(xiàn)向iframe中表單傳值的方法,涉及js針對頁面元素及表單屬性操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03
微信公眾號網(wǎng)頁授權(quán)登錄的超簡單實(shí)現(xiàn)步驟
微信開放平臺為第三方移動程序提供分享、傳播的接口,使用戶可將第三方程序的內(nèi)容發(fā)布給好友或分享至朋友圈,下面這篇文章主要給大家介紹了關(guān)于微信公眾號網(wǎng)頁授權(quán)登錄的超簡單實(shí)現(xiàn)方法,需要的朋友可以參考下2022-06-06
微信小程序之高德地圖多點(diǎn)路線規(guī)劃過程示例詳解
這篇文章主要介紹了微信小程序之高德地圖多點(diǎn)路線規(guī)劃過程示例詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
前端根據(jù)后端返回的文本流逐個展示文本內(nèi)容代碼示例
前端如何根據(jù)后端返回的文本流逐個展示文本內(nèi)容的實(shí)現(xiàn)步驟,前端調(diào)用特定方法來獲取文本流,然后通過處理這些文本流,逐個在界面上展示文本內(nèi)容,需要的朋友可以參考下2025-01-01
javascript 函數(shù)調(diào)用規(guī)則
構(gòu)造器函數(shù)以大寫字母開頭是一個好的習(xí)慣,這可以作為一個提醒,讓你在調(diào)用的時候不要忘記前面的new運(yùn)算符.2009-08-08

