JavaScript STL, Part 4 | 2
Object Oriented Javascript - Part 1
To many object orientation purists, a programming language doesn't cut the mustard unless it supports some form of class inheritance - where one class can 'inherit' the behaviour of another class. In this article you'll learn how JavaScript can support class inheritance for user defined classes and so qualify as an oject oriented language.
A Bit About JavaScript Classes
In JavaScript, everything you interact with is an object of some kind; from the strings, numbers and arrays used in the script to the functions that are executed. This is more than just a vague notion of objectness, in JavaScript, everything is an Object. The JavaScript Object class is automatically inherited by every other object. This means that the methods and properties of the Object class (see Table 1 below) are supported and implemented by all objects within JavaScript.
Table 1: Properties and Methods of Object.
Property/Method | Description |
---|---|
prototype | Provides a base set of functionality for an object |
constructor | Specifies the function that creates an object |
propertyIsEnumerable | Indicates whether a property is part of an object and is enumerable |
isPrototypeOf() | Determine whether two objects are related |
hasOwnProperty() | Indicates whether an object has a named property |
toLocaleString() | Output to string in a locale specific format |
toString() | Output value of object to string |
valueOf() | Return the value of the object |
It is the prototype property that forms the basis of this inheritance. When searching for a property or method on an object, if not found attached to the object instance itself, the object's prototype is searched. Now the clever part is that like everything else in JavaScript, the prototype is an Object, and its class will have a prototype of its own. If a property or method is not found in an object instance or its prototype, JavaScript will move on to the prototype's prototype, and so on until it reaches the Object class itself.
A Means to Inherit
This powerful feature of prototype chains (leading from a user defined JavaScript class upwards to the Object prototype) enables a simple means to support class inheritance. It's all done with a subtle manipulation of the prototype property...
// create a base class with one method
function Base(b)
{
this.b = b;
}
Base.prototype.foo = function()
{
alert('foo');
}
// create a derived class inheriting from Base
function Derived(d)
{
this.d = d;
}
// 'inherit' from Base by assigning an instance of Base to the prototype
Derived.prototype = new Base();
Derived.prototype.constructor = Derived;
// create a method for the Derived class
Derived.prototype.bar = function()
{
alert('bar');
}
// create a Base instance
var b = new Base(1);
b.foo(); // calls Base.prototype.foo()
b.bar(); // error: not incuded in Base.prototype/
// create a Derived instance
var d = new Derived(2);
d.foo(); // calls Base.prototype.foo()
d.bar(); // calls Derived.prototype.bar()
In this code, two classes are declared called Base and Derived. The Derived class inherits from Base by creating an instance of Base and assigning it to its prototype. This way any methods and properties available to the Base class are automatically available to instances of Derived.
Created:
March 27, 2003
Revised: February 17, 2006
URL: https://webreference.com/programming/javascript/gr/column18/1