zInherit库下载地址:http://www.nczonline.net/downloads/ 演示代码: <html> <head> <title>Example</title> <script type="text/javascript" src="zinherit.js"></script> </head> <body> <script type="text/javascript"> function Polygon(iSides) { this.sides = iSides; } Polygon.prototype.getArea = function () { return 0; }; function Triangle(iBase, iHeight) { Polygon.call(this, 3); this.base = iBase; this.height = iHeight; } Triangle.prototype.inheritFrom(Polygon); Triangle.prototype.getArea = function () { return 0.5 * this.base * this.height; }; function Rectangle(iLength, iWidth) { Polygon.call(this, 4); this.length = iLength; this.width = iWidth; } Rectangle.prototype.inheritFrom(Polygon); Rectangle.prototype.getArea = function () { return this.length * this.width; }; var triangle = new Triangle(12, 4); var rectangle = new Rectangle(22, 10); alert(triangle.sides); alert(triangle.getArea()); alert(rectangle.sides); alert(rectangle.getArea()); alert(triangle.instanceOf(Triangle)); alert(triangle.instanceOf(Polygon)); alert(rectangle.instanceOf(Rectangle)); alert(rectangle.instanceOf(Polygon)); </script> </body> </html> 动态原型的支持: <html> <head> <title>Example</title> <script type="text/javascript" src="zinherit.js"></script> </head> <body> <script type="text/javascript"> function Polygon(iSides) { this.sides = iSides; if (typeof Polygon._initialized == "undefined") { Polygon.prototype.getArea = function () { 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); Triangle.prototype.getArea = function () { return 0.5 * this.base * this.height; }; Triangle._initialized = true; } } function Rectangle(iLength, iWidth) { Polygon.call(this, 4); this.length = iLength; this.width = iWidth; if (typeof Rectangle._initialized == "undefined") { Rectangle.prototype.inheritFrom(Polygon); Rectangle.prototype.getArea = function () { return this.length * this.width; }; Rectangle._initialized = true; } } var triangle = new Triangle(12, 4); var rectangle = new Rectangle(22, 10); alert(triangle.sides); alert(triangle.getArea()); alert(rectangle.sides); alert(rectangle.getArea()); alert(triangle.instanceOf(Triangle)); alert(triangle.instanceOf(Polygon)); alert(rectangle.instanceOf(Rectangle)); alert(rectangle.instanceOf(Polygon)); </script> </body> </html> 多重继承支持:(原型链不支持) <html> <head> <title>Example</title> <script type="text/javascript" src="zinherit.js"></script> </head> <body> <script type="text/javascript"> function ClassX() { this.messageX = "This is the X message."; if (typeof ClassX._initialized == "undefined") { ClassX.prototype.sayMessageX = function () { alert(this.messageX); }; ClassX._initialized = true; } } function ClassY() { this.messageY = "This is the Y message."; if (typeof ClassY._initialized == "undefined") { ClassY.prototype.sayMessageY = function () { 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 = function () { alert(this.messageZ); }; ClassZ._initialized = true; } } var objZ = new ClassZ(); objZ.sayMessageX(); objZ.sayMessageY(); objZ.sayMessageZ(); </script> </body> </html>