Chapter 9. Classes, Constructors, and Prototypes


JavaScript objects were introduced in Chapter 7. That chapter treated each object as a unique set of properties, different from every other object. In many object-oriented programming languages, it is possible to define a class of objects and then create individual objects that are instances of that class. You might define a class named Complex to represent and perform arithmetic on complex numbers, for example. A Complex object would represent a single complex number and would be an instance of that class.

JavaScript does not support true classes the way that languages like Java, C++, and C# do.[*] Still, however, it is possible to define pseudoclasses in JavaScript. The tools for doing this are constructor functions and prototype objects. This chapter explains constructors and prototypes and includes examples of several JavaScript pseudo-classes and even pseudosubclasses.

[*] True classes are planned for JavaScript 2.0, however.

For lack of a better term, I use the word "class" informally in this chapter. Be careful, however, that you don't confuse these informal classes with the true classes of JavaScript 2 and other languages.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net