Object-Oriented Programming with JavaScript, Part I: Inheritance

Probing for Inheritance in NS

In Netscape Navigator 4.x and Netscape 6, JavaScript stores the prototyping relationship in an internal property of the object, __proto__ (two underscore characters at the front and two at the end.) Let's take an example:

The object myPicture has an internal property, __proto__, which is set to Shape when the statement:

Square.prototype = new Shape;

is executed. The value of __proto__ instructs JavaScript where to look for properties, when local definitions are not available. In the example above, the property borderWidth is not defined within the object Square. The browser looks for the value of __proto__, which is Shape, and then fetches the value of its borderWidth property. Being an internal property, most browsers will return undefined when asked to alert __proto__. Netscape 6, though, returns [object Object]. Try it.

Let's look at another example. In this example, UniversityAvenue inherits from Street, which inherits from City, which inherits from State: