当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 给Function做的OOP扩展

Javascript
玩透弹出窗口
几个常用的日期函数
简单的脚本帮你编排JScript程序中的缩进
得到 words.js?hello,world! 参数的处理方法
如何在javascript中传值
可输入的select
IE支持的HTML元素的DISABLE属性在NETSCAPE4.76中的实现
利用xml数据岛实现多级关联下拉选择框的做法
利用Wipe等ActiveX技术,实现n(n>>2)幅图片轮换擦洗显示
Javascript技术实现真正的网上试听
JavaScript实现在线编辑表格
根据客户端的分辨率不同而重定向到不同网页的脚本
几种不刷新页面取数据的方法
web进度条
随手写的一个动态添加删除行的HTC行为组件
农历与阳历的对照
关于在页面中解决打印的几个问题
"打开,另存为,属性,打印"等14个JS代码
无提示框关闭IE窗口
实现上传(增删)多个文件的客户端写法。

Javascript 中的 给Function做的OOP扩展


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 132 ::
收藏到网摘: n/a

自编框架Megami的一部分……现在发现自己实在太宅了…… 文件中出现的名字请对号入座……
复制代码 代码如下:

// 下面是OOP用的方法
// 这样很猥琐……因为JS并不是OOP语言……
// 但伟大的伍迷指引我们来这么干
// Belldandy会保佑用这些方法来OOP的人的……
Function.prototype.inherits = function(base){
//派生关系,保留了prototype
//只支持单派生
this.prototype = new base();
return this;
}
Function.prototype.create = function(){
//类的创建器,和用new等价
//JS不支持在构造器用call和apply,所以……
//Belldandy啊,感谢你告诉我怎么解决这个问题啊……
var _args = [];
for(i=0;i<arguments.length;i++) _args.push('arguments['+i+']');
return eval('new this('+_args.join(',')+')'); //eval都用上了……Bell啊,下次给个好点的主意吧……
}
Function.prototype.pin = function(pinner,args){
// 注册服务,或者叫“pin”服务
// EventManager就可以这么干
// 你也可以认为实现了有默认实现的接口……
// 例如,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+"']; /* 没有设置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啊,饶恕我吧,虽然这样不产生闭包
return this;
}
Function.prototype.static = function(name,value){ //静态特征,包括属性和方法
this[name] = value;
return this;
}

使用效果如下:
复制代码 代码如下:

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自己参阅月影的书