//闭包策略
(function (){
DW = {
version:'1.0'
};
DW.extend = function(o,c){
//定义对象f并返回,在new时使用;
var f = function(){
//用arguments目的是使传入的参数变量可以为任意变量名,用arguments[0]方式获得变量;
this.config = arguments[0];
for(var i = 1;i <= this.superCount;i++){
//用以下方式代替 eval执行方法,提高效率;
var propName = 'superInit' + i;
this[propName]();//加()执行方法;
}
}
var d = DW.copy(o.prototype);
for(var p in d){
f.prototype[p] = d[p];
}
d = DW.copy(c);
for(var p in d){
f.prototype[p] = d[p];
}
if(c.init){
var i = f.prototype.superCount = o.prototype.superCount+1;
//用以下方式代替 eval赋值,提高效率;
var propName = 'superInit'+i;
f.prototype[propName] = c.init;
}
return f;
};
//二次开发修改js对象时使用,在new对象前调用覆写
DW.override = function(o,c){
var d = DW.copy(c.prototype);
for( var p in d){
o.prototype[p] = d[p];
}
};
DW.copy = function(c){
var o = {};
for(var p in c){
//递归深层拷贝;
o[p] = typeof c[p] === 'object' ? DW.copy(c[p]) : c[p];
}
return o;
};
DWObject = function(){};
DWObject.prototype.superCount = 0;
//定义的属性需要放在init中初始化,确保继承后每个父对象的属性初始化,并且初始化时间点可控制;
DWObject.prototype.init = function(){};
})();
javascript 继承框架DW
最新推荐文章于 2022-04-26 12:53:38 发布