1、zInherit.js
/**http://www.nczonline.net/downloads*5、zInherit:使用zInherit.js库,给Object添加了两个方法:inheritFrom()和instanceOf()*instanceof()只是instanceof运算符的替代品,不适用原型链**//**baseClass多边形**/function Polygon(iSides) { this.sides = iSides;}Polygon.prototype.getArea = function() { return 0;}/**三角形**/function Triangle(iBase, iHeight) { Polygon.call(this, 3); //3条边 this.base = iBase; this.height = iHeight;}Triangle.prototype.inheritFrom(Polygon); //继承Triangle.prototype.getArea() { //覆盖Polygon的getArea() return 0.5 * this.base * this.height;}/**平行四边形**/function Rectangle(iLength, iWidth) { Polygon.call(this, 4); this.width = iWidth; this.length = iLength;}Rectangle.prototype.inheritFrom(Polygon); //继承Rectangle.prototype.getArea() { //覆盖Polygon的getArea() return this.length * this.width;}/**特性一:动态原型支持*可避开原型链的限制,实现动态原型**/function Polygon(iSides) { this.sides = iSides; if (typeof Polygon._initialized == "undefined") { Polygon.prototype.getArea() { return 0; } Polygon._initialized = true; }}function Triangle(iBase, iHeight) { Polygon.call(this, 3); this.base = iBase; this.height = iHeight; if (typeof Triangle._initialized == "undefined") { Triangle.prototype.inheritFrom(Polygon); //使用inheritFrom()方法不重写prototype对象,只是为其加入方法而已 Triangle.prototype.getArea() { return 0.5 * this.base * this.height; } Triangle.prototype._initialized = true; }}/**特性二:多继承支持*inheritFrom()方法不替换prototype对象*要继承属性和方法,inheritFrom()必须与对象冒充一起使用**/function ClassX() { this.messageX = "this is the X message"; if (typeof ClassX._initialized = "undefined") { ClassX.prototype.sayMessageX() { alert(this.messageX); } } ClassX._initialized = true;}function ClassY() { this.messageY = "this is the Y message"; if (typeof ClassY._initialized = "undefined") { ClassY.prototype.shadowOffsetY() { alert(this.messageY); } } ClassY._initialized = true;}function ClassZ() { ClassX.apply(this); ClassY.apply(this); this.messageZ = "this is the Z message"; if (typeof ClassZ._initialized = "undefined") { ClassZ.prototype.inheritFrom(ClassX); ClassZ.prototype.inheritFrom(ClassY); ClassZ.prototype.sayMessageZ() { alert(this.messageZ); } } ClassZ._initialized = true;}2、xbObject.js
/**http://devedge.netscape.com/*6、xbObject*不止支持继承,还支持方法的重载和调用超类方法的能力**///定义超类:_classes.registerClass("Grandpa");function Grandpa(pName) { _classes.defineClass("Grandpa", prototypeFunction); this.init(pName); //init方法接受和构造函数相同的方法 function prototypeFunction() { Grandpa.prototype.init = function(pName) { this.parentMethod("init"); //parentMethod方法可以传递多个参数,第一个参数为要调用的父类方法名字,然后其他参数被传递给此方法 this.name = pName; } Grandpa.prototype.saynName = function() { alert(this.name); } }}//定义继承: _classes.registerClass("Parent", "Grandpa");function Parent(pName, cNumber) { _classes.defineClass("Parent", prototypeFunction); this.init(pName, cNumber); //init方法接受和构造函数相同的方法 function prototypeFunction() { Parent.prototype.init = function(pName, cNumber) { this.parentMethod("init"); this.name = pName; this.number = cNumber; } Parent.prototype.sayMyName = function() { alert(this.name); } Parent.prototype.sayMyNumber = function() { alert(this.number); } }}/**xbObject:重载多边形***/_classes.registerClass("Polygon");function Polygon(iSides) { _classes.defineClass("Polygon", prototypeFunction); this.init(iSides); function prototypeFunction() { Polygon.prototype.init = function(iSides) { this.parentMethod("init"); this.sides = iSides; } Polygon.prototype.getArea() { return 0; } }}_classes.registerClass("Triangle", "Polygon");function Triangle(ibase, iHeight) { _classes.defineClass("Triangle ", prototypeFunction); this.init(iBase, iHeight); function prototypeFunction() { Triangle.prototype.init = function(ibase, iHeight) { Triangle.parentMethod("init", 3); this.base = iBase; this.height = iHeight; } Triangle.getArea() { return this.base * this.height; } }}_classes.registerClass("Rectangle", "Polygon");function Rectangle(iLength, iWidth) { _classes.registerClass("Rectangle", "Polygon"); this.init(iLength, iWidth); function protoypeFunction() { Rectangle.prototype.init = function(iLength, iWidth) { Rectangle.parentMethod("init", 4); this.length = iLength; this.width = iWidth; } Rectangle.prototype.getArea() { return this.height * this.width; } }}