spacer

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

home / programming / javascript / professional / chap3 / 3 123
[next]
Developer News
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC

Professional JavaScript

Implementing JavaScript Inheritance

JavaScript is weakest in its object-oriented support when it comes to inheritance. The features are there for you to do it, but unless you're organized, nothing will work as you expect. In order to see the issues, first a bit of theory about objects, classes, and instances.

Understanding Classes and Instances

If you have an object, then usually there are some properties containing data or methods in it. However it is also usually possible to write down some information about that object. At the simplest you can write down its name (from the constructor function perhaps) and the number and type of properties it has. In object-oriented lingo, that kind of information is called an object class definition. A class definition has the same role for an object that a type has for a primitive bit of data such as a number. "Class definition" and "object type" are interchangeable ideas. An actual object itself is called an object instance. Instance just means "one of" that type of object. Therefore it's common to see many object instances for a given object class. Recall Chapter 2 describes how to use a JavaScript object constructor to create as many objects as you like.

There is a general expectation in object-oriented languages that once the details of an object class are settled they won't change. Individual bits of data in an object instance might change, but not the class. This expectation comes from strongly typed languages like C++ and Java. In those languages, it's not a basic process to get at the information in an object class; you have to use an extra-special mechanism (like Run Time Type Information [RTTI] or Reflection) and you certainly can't change an object class once it exists. This is also consistent with the way primitive types work – you can easily change a variable holding a particular value of type Number (say 5) into a different value (say 6), but you can't change the Number type itself (say, by increasing the range of allowed numbers, or by banning negative numbers). If you did that, then maybe you would call the changed type BigPositiveNumber to distinguish it from the normal Number type.

This all helps to understand how objects that inherit information are managed. This diagram illustrates the pieces at work:

text1

Notice how the classes and instances are nicely divided – instances exist (like speech) but classes are more abstract (like thought). In strongly typed languages like C++ and Java the two rarely meet, and classes are never modified.

In JavaScript, the picture is more like this:

text

In JavaScript, classes are ordinary objects too – created when you make a constructor function. That means you can accidentally damage classes in your program. Or you can deliberately change classes. It also makes it easy to mix up class objects and "ordinary" instance objects. In fact the whole idea of separate, abstract classes breaks down a bit in JavaScript.

Because of this breakdown, it's better to focus on prototypes, which is the mechanism available. However, it pays to understand classes and instances. Both are implemented with the same mechanism (JavaScript Objects) and both are modifiable, so the objects available can become extremely fluid (and unpredictable). Sometimes it pays to be able to think in class/instance terms; but if you're a real computer guru then sometimes you'll want to throw out the class/instance split entirely.

There is one rock to cling to. A JavaScript object doesn't define host objects – they are defined elsewhere. Therefore you can nearly always be sure that host object definitions will never, ever, change.

In the above diagram there are some question marks. In particular, what does the prototype property get set to when you're trying to get rectangles to inherit from polygons? It turns out that there are two main possibilities.

home / programming / javascript / professional / chap3 / 3 123
[next]

Copyright 1999 (1st Edition) and 2001 (2nd Edition) Wrox Press Ltd. and

JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
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
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
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
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data · Controllers: Programming Application Logic
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Sprint Launches Mobile WiMAX Network · Albatron Downsizes with the KI780G Mini-ITX Motherboard · Can't Find a Wi-Fi Network? Make Your Own.


Created: February 12, 2001
Revised: February 12, 2001


URL: http://webreference.com/programming/javascript/professional/chap3/