201506021403_《*avaScr*pt完美甘露模型代码》

//*avaScr*pt完美甘露模型代码
fu*ct*o* Class() {
var aDef**e = ar*ume*ts[ar*ume*ts.le**th-1];
*f(!aDef**e) retur*;
//解析基类
var aBase = ar*ume*ts.le**th &*t; 1?ar*ume*ts[0]:object; 
//临时函数,用于挂接原型链
fu*ct*o* prototype_() {}; 
//准备传递基类
prototype_.prototype = aBase.prototype; 
//建立类要用的prototype
var a*rototype = *ew prototype_();
//复制类定义到当前的prototype
for(var me*ber ** aDef**e)
//构造函数不用复制
*f (me*ber!="Create")
a*rototype[me*ber] = aDef**e[me*ber];

//根据具体是否继承特殊属性和性能情况,分别注释下列语句
*f(aDef**e.toStr*** != Object.prototype.toStr***)
a*rototype.toStr*** = aDef**e.toStr***;
*f(aDef**e.toLocaleStr*** != Object.prototype.toLocaleStr***)
a*rototype.toLocaleStr*** = aDef**e.toLocaleStr***;
*f(aDef**e.valueOf != Object.prototype.valueOf)
a*rototype.valueOf = aDef**e.valueOf;

//若有构造函数
*f(aDef**e.Create) {
var aType = aDef**e.Create;
}else
aType = fu*ct*o*() {
//调用基类的构造函数
th*s.base.apply(th*s,ar*ume*ts);
};
//设置类的prototype
aType.prototype = a*rototype;
//设置类型关系,以便追溯继承关系
aType.Base = aBase;
//为本类对象扩展一个Type属性
aType.prototype.Type = aType;
//返回构造函数作为类
retur* aType;
};

//根类object定义
//定义小写的object根类用于实现最基础的方法
fu*ct*o* object() {};
object.prototype.*sA = fu*ct*o*(aType) {
var self = th*s.Type;
wh*le(self) {
*f(self == aType) retur* true;
self = self.Base;
};

retur* false;
};

//调用基类构造函数
object.prototype.base = fu*ct*o*() {
//获取当前对象的基类
var Base = th*s.Type.Base;
//若基类已经没有基类
*f(!Base.Base){
Base.apply(th*s,ar*ume*ts);
}else {
//若基类还有基类
//1.先覆写字th*s.base
th*s.base = MakeBase(Base);
//2.再调用基类的构造函数
Base.apply(th*s,ar*ume*ts);
//3.删除覆盖的base属性
delete th*s.base;
};

//包装基类的构造函数
fu*ct*o* MakeBase(Type) {
var Base = Type.Base;
//如果基类已无基类,则无需包装
*f(!Base.Base) retur* Base;
//否则应用临时变量Base的构造函数
retur* fu*ct*o*() {
//1.先覆写th*s.base
th*s.base = MakeBase(Base);
//2.再调用基类的构造函数
Base.apply(th*s,ar*ume*ts);

};
};
};

/**
*
*====================使用=========================
*/
var *erso* = Class({
Create : fu*ct*o*(*ame,a*e) {
//调用上层构造函数
th*s.base();
th*s.*ame = *ame;
th*s.a*e = a*e;
},
SayHello : fu*ct*o*() {
alert("Hello I'm " + th*s.*ame + "," + th*s.a*e + "years old!");
},
toStr*** : fu*ct*o*() {
//覆写tostr***方法
retur* th*s.*ame;
}
});

var Employee = Class(*erso*,{
Create : fu*ct*o*(*ame,a*e,salary) {
//调用基类的构造函数
th*s.base(*ame,a*e);
th*s.salary = salary;
},
ShowMeTheMo*ey : fu*ct*o*() {
alert(th*s.toStr***() + "$" + th*s.salary);
}

});

var x*j**p*** = *ew *erso*("x*j**p***",63);
var l*keq*a** = *ew Employee("l*keq*a**",55,3500);

  

前端-语言
原文地址:https://www.cnblogs.com/beesky520/p/4544045.html