JavaScript/Inheritance

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: Constructors and Prototypes Index Next: Access Control

[edit] instanceof operator

The instanceof operator determines if an object was instantiated as a child of another object, returning true if this was the case.

[edit] Inheritance by Prototypes

The prototype of an object can be used to create fields and methods for an object. This prototype can be used for inheritance by assigning a new instance of the superclass to the prototype.

function CoinObject()
{
    this.value = 0; 
    this.diameter = 1;
}
 
function Penny()
{
    this.value=1;
}
Penny.prototype = new CoinObject();
 
function Nickel()
{
    this.value=5;
}
Nickel.prototype = new CoinObject();

[edit] Inheritance by functions

TODO

TODO
Provide a recommended method for function-based inheritacnce - one reference is http://www.crockford.com/javascript/inheritance.html

function CoinObject()
{
    this.value = 0; 
    this.diameter = 1;
}
Previous: Constructors and Prototypes Index Next: Access Control