Chapter 4 - JavaScript中继承的实现
[IT168技术文档]我的Javascript终于变成了面向对象的语言了!
Javascript通过 1.对象冒充 2.原型链 3.混合方式(用对象冒充来继承父类的属性,用原型链来继承父类的方法) 来实现了继承。动态原型的方法不使用第三方库的情况下不能实现继承功能,不过通过zInherit库(http://www.nczonline.net/downloads)还是可以的,不过偶不喜欢这种多写判断语句的方法。
直接看混合方式了。
Javascript通过 1.对象冒充 2.原型链 3.混合方式(用对象冒充来继承父类的属性,用原型链来继承父类的方法) 来实现了继承。动态原型的方法不使用第三方库的情况下不能实现继承功能,不过通过zInherit库(http://www.nczonline.net/downloads)还是可以的,不过偶不喜欢这种多写判断语句的方法。
直接看混合方式了。
原文地址function Polygon(sides) { this.sides = sides; } Polygon.prototype.getArea = function() { return 0; } function Triangle(base, height) { Polygon.call(this, 3); this.base = base; this.height = height; } Triangle.prototype = new Polygon(); Triangle.prototype.getArea = function() { return 0.5 * this.base * this.height; } function Rectangle(length, width) { Polygon.call(this, 4); this.length = length; this.width = width; } Rectangle.prototype = new Polygon(); Rectangle.prototype.getArea = function() { return this.length * this.width; } function Square(length) { Rectangle.call(this, length); } Square.prototype = new Rectangle(); Square.prototype.getArea = function() { return this.length * this.length; } function main() { var polygon = new Polygon(0); alert("Polygon " + polygon.sides + " area:" + polygon.getArea()); var triangle = new Triangle(4, 4); alert("Triangle " + triangle.sides + " area:" + triangle.getArea()); var rectangle = new Rectangle(4, 5); alert("Rectangle " + rectangle.sides + " area:" + rectangle.getArea()); var square = new Square(4); alert("Square " + square.sides + " area:" + square.getArea()); alert("Triangle is a kind of Polygon - " + (triangle instanceof Polygon)); alert("Rectangle is a kind of Polygon - " + (rectangle instanceof Polygon)); alert("Square is a kind of Polygon - " + (square instanceof Polygon)); alert("Square is a kind of Rectangle - " + (square instanceof Rectangle)); alert("Square is a kind of Triangle - " + (square instanceof Triangle)); alert("Rectangle is a kind of Triangle - " + (rectangle instanceof Triangle)); } 使用了zInherit库可以实现多重继承 function ClassA(color) { this.color = color; } ClassA.prototype.sayColor = function () { alert(this.color); } function ClassB(size) { this.size = size; } ClassB.prototype.saySize = function () { alert(this.size); } function ClassC(color, size, font) { ClassA.call(this, color); ClassB.call(this, size); this.font = font; } ClassC.prototype.inheritFrom(ClassA); ClassC.prototype.inheritFrom(ClassB); ClassC.prototype.sayFont = function() { alert(this.font); } function main() { var objc = new ClassC('blue', 12, 'sans'); objc.sayColor(); objc.saySize(); objc.sayFont(); alert(objc.instanceOf(ClassA)); alert(objc.instanceOf(ClassB)); alert(objc.instanceOf(ClassC)); } 用xbObjects(http://archive.bclary.com/xbProjects-docs/xbObject)可以给JavaScript提供了继承,方法重载和调用超类的方法的能力 _classes.registerClass("Polygon"); function Polygon(sides) { _classes.defineClass("Polygon", prototypeFunction); this.init(sides); function prototypeFunction() { Polygon.prototype.init = function(sides) { this.parentMethod("init"); this.sides = sides; } Polygon.prototype.getArea = function() { return 0; } } } _classes.registerClass("Triangle", "Polygon"); function Triangle(base, height) { _classes.defineClass("Triangle", prototypeFunction); this.init(base, height); function prototypeFunction() { Triangle.prototype.init = function(base, height) { this.parentMethod("init", 3); this.base = base; this.height = height; } Triangle.prototype.getArea = function() { return 0.5 * this.base * this.height; } } } _classes.registerClass("Rectangle", "Polygon"); function Rectangle(length, width) { _classes.defineClass("Rectangle", prototypeFunction); this.init(length, width); function prototypeFunction() { Rectangle.prototype.init = function(length, width) { this.parentMethod("init", 4); this.length = length; this.width = width; } Rectangle.prototype.getArea = function() { return this.length * this.width; } } } _classes.registerClass("Square", "Rectangle"); function Square(length, width) { _classes.defineClass("Square", prototypeFunction); this.init(length); function prototypeFunction() { Square.prototype.init = function(length, width) { this.parentMethod("init"); this.length = length; this.width = width; } Square.prototype.getArea = function() { return this.length * this.length; } } } function main() { var polygon = new Polygon(0); alert("Polygon " + polygon.sides + " area:" + polygon.getArea()); var triangle = new Triangle(4, 4); alert("Triangle " + triangle.sides + " area:" + triangle.getArea()); var rectangle = new Rectangle(4, 5); alert("Rectangle " + rectangle.sides + " area:" + rectangle.getArea()); var square = new Square(4); alert("Square " + square.sides + " area:" + square.getArea()); alert("Triangle is a kind of Polygon - " + (triangle.isInstanceOf(Polygon))); alert("Rectangle is a kind of Polygon - " + (rectangle.isInstanceOf(Polygon))); alert("Square is a kind of Polygon - " + (square.isInstanceOf(Polygon))); alert("Square is a kind of Rectangle - " + (square.isInstanceOf(Rectangle))); alert("Square is a kind of Triangle - " + (square.isInstanceOf(Triangle))); alert("Rectangle is a kind of Triangle - " + (rectangle.isInstanceOf(Triangle))); }
0
相关文章