JavaScript supports an inheritance chain based on prototypes. Every constructor has an associated prototype and every object created with that constructor has a link to the prototype as an integral part of its instantiation. This is called the object's prototype.
A prototype may have an implicit reference to it's parent prototype. This provides inheritance through a prototype chain that is analogous to the super-class and sub-class mechanisms in a class-based object-oriented language.
This allows for properties to be overridden or provided by parent prototypes if the properties are not implemented in the target object.
In a class-based object-oriented environment, as a general rule, state values are embodied in object instances of a class but methods are contained in the classes themselves so the inheritance only projects structure and behavior down through the sub-classing mechanism.
In the prototype-based inheritance the state AND methods are carried by the objects so structure, state and behavior are all inherited down the prototype chain.
All objects that do not contain a particular property and that are descended via the inheritance tree from a single object that does contain that property will all share that one single property instance.
Prev | Home | Next |
protected | Up | Prototype chain |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |