spacer
home / programming / javascript / gr / column18 / 1 current pageTo page 2To page 3
[next]

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/MethodDescription
prototypeProvides a base set of functionality for an object
constructorSpecifies the function that creates an object
propertyIsEnumerableIndicates 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.

home / programming / javascript / gr / column18 / 1 current pageTo page 2To page 3
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: March 27, 2003
Revised: February 17, 2006

URL: http://webreference.com/programming/javascript/gr/column18/1