首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JavaScript Object-Oriented Programming - Inheritence

2012-07-25 
JavaScript Object-Oriented Programming -- InheritenceThe concept most often discussed in relation t

JavaScript Object-Oriented Programming -- Inheritence

The concept most often discussed in relation to OO programming is inheritance. Many OO languages support two types of inheritance: interface inheritance, where only the method signatures are inherited, and implementation inheritance, where actual methods are inherited. Interface inheritance is not possible in ECMAScript, because functions do not have signatures. Implementation inheritance is the only type of inheritance supported by ECMAScript, and this is done primarily through the use of prototype chaining.

?

Prototype Chaining

ECMA-262 describes prototype chaining as the primary method of inheritance in ECMAScript. The basic idea is to use the concept of prototypes to inherit properties and methods between two reference types. Recall the relationship between constructors, prototypes, and instances: each constructor has a prototype object that points back to the constructor, and instances have an internal pointer to the prototype. What if the prototype were actually an instance of another type? That would mean the prototype itself would have a pointer to a different prototype that, in turn, would have a pointer to another constructor. If that prototype were also an instance of another type, then the pattern would continue, forming a chain between instances and prototypes. This is the basic idea behind prototype chaining.

?

In this example, the SuperType constructor defines two properties, name and colors, and the SuperType prototype has a single method called sayName(). The SubType constructor calls the SuperType constructor, passing in the name argument, and defines its own property called age. Additionally, the SubType prototype is assigned to be an instance of SuperType, and then a new method called sayAge() is defined. With this code, it’s then possible to create two separate instances of SubType that have their own properties, including the colors property, but all use the same methods.

Addressing the downsides of both prototype chaining and constructor stealing, combination inheritance is the most frequently used inheritance pattern in JavaScript. It also preserves the behavior of instanceof and isPrototypeOf() for identifying the composition of objects.

?

Here gives a?comprehensive example about?Combination Inheritance.

?

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Combination Inheritance Example</title><script type="text/javascript">function Shape(edge) {this.edge = edge;}Shape.prototype.getArea = function() {return -1;}Shape.prototype.getEdge = function() {return this.edge;}function Triangle(bottom, height) {Shape.call(this, 3);this.bottom = bottom;this.height = height;}Triangle.prototype = new Shape();Triangle.prototype.getArea = function() {return 0.5 * this.bottom * this.height;}//Triangle.prototype.getEdge = function()//{//return this.edge;//}var triangle = new Triangle(10, 4);//alert(triangle.getEdge());//alert(triangle.getArea());function Rectangle(bottom, height) {Shape.call(this, 4);this.bottom = bottom;this.height = height;}Rectangle.prototype = new Shape();Rectangle.prototype.getArea = function() {return this.bottom * this.height;}//Rectangle.prototype.getEdge = function()//{//return this.edge;//}var rectangle = new Rectangle(20, 40);alert(rectangle.getEdge());alert(rectangle.getArea());</script></head><body></body></html>

热点排行