Inheritance through Prototypes - www.docjavascript.com | WebReference

Inheritance through Prototypes - www.docjavascript.com


Inheritance through Prototypes

We touched upon JavaScript's object-oriented nature several times in our previous columns. In fact, the words "Java" and "JavaScript" are often associated with OO programming and OO languages. In this column we show you how to take advantage of some of the object-oriented characteristics of the language. In particular, we show you how to extend objects with the prototype property.

You can extend two types of objects. First, you can extend a class of objects. You can enhance, for example, the Array class, thus affecting all existing and future instances of the Array class of objects. Secondly, you can extend a particular instance of an object, not affecting any other objects of the same class. There are several ways to extend an object. You can add properties, methods, and other objects. When adding a new object to an existing one, all of the new object's properties and methods are added. Two objects can be extended by a common third object. They "inherit" the characteristics of the same object.

In this column you'll learn:


We demonstrate the inheritance concept by two animation scripts we presented in one of our previous column, JavaScript Animations, Part II. We modified the simple animation and the nested animation scripts to inherit a common object. At this point, you may view both the code for simple animation as well as the code for the nested one. You may also view the code for the shared animation library, or download its zipped version.

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

All Rights Reserved. Legal Notices.
Created: January 18, 1999
Revised: January 18, 1999

URL: http://www.webreference.com/js/column34/