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