spacer

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

home / experts / javascript / column77


Netscape 6, Part VI: Object-Oriented DOCJSLIB 1.2

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

Class Inheritance

Inheritance is a very important requirement from an object-oriented language. JavaScript does not support any OO primitives in its language, but you can implement most of the stuff you need. Let's look at the following example:

function superClass() {

  this.bye = superBye;
  this.hello = superHello;
  
  function superHello() {
    return "Hello from superClass";
  }
  
  function superBye() {
    return "Bye from superClass";
  }
}

function subClass() {
  this.inheritFrom = superClass;
  this.inheritFrom();
  this.bye = subBye;
  
}

function subBye() {
  return "Bye from subClass";
}

var newClass = new subClass();
alert(newClass.bye());
alert(newClass.hello());

We have a super class , superClass, and a sub class, subClass. superClass defines two methods: bye() and hello(). subClass inherits these two methods by:

this.inheritFrom = superClass;
this.inheritFrom();

and overrides the bye() method. When clicking here, you trigger the evaluation of bye() and hello() from the sub class. You can judge from the alert boxes that indeed the hello() method is as defined by the super class, while the bye() method is as overridden by the sub class.

Next: How to architect the include files

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: February 12, 2001
Revised: February 12, 2001

URL: http://www.webreference.com/js/column77/3.html