spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column79


Object-Oriented Programming with JavaScript, Part I: Inheritance

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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:

function Shape() {
  this.borderWidth = 5;
}

function Square() {
  this.edge = 12;
}

Square.prototype = new Shape;

myPicture = new Square;

alert(myPicture.__proto__);
alert(myPicture.borderWidth);

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:

function State() {
}

function City() {
}
City.prototype = new State;

function Street() {
}

Street.prototype = new City;

var UniversityAvenue = new Street();

function tryIt() {
  alert(UniversityAvenue.__proto__== Street.prototype);
  alert(UniversityAvenue.__proto__.__proto__==
   City.prototype);
  alert(UniversityAvenue.__proto__.__proto__.__proto__
   == State.prototype);
  alert(UniversityAvenue.__proto__.__proto__.__proto__.
   __proto__== Object.prototype);
  alert(UniversityAvenue.__proto__.__proto__.__proto__.
   __proto__.__proto__== null);
}

All alert boxes will yield a value of true in Netscape Navigator 4.x and Netscape 6. Try it. __proto__ is not supported by Internet Explorer.

Next: How to emulate instanceOf()

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: March 12, 2001
Revised: March 12, 2001

URL: http://www.webreference.com/js/column79/6.html