2.2. JavasScript Is a Prototype Rather than OO Language

It is temping to think of Point as a class that we have defined, but that is not really the case. Point is actually simply a reference to a function, and we could quite as easily defined the instance as:

var myPoint = new Object();
myPoint["x"]           = 0;
myPoint["y"]           = 0;
myPoint["toString"]    = Point_toString;
myPoint["constructor"] = Point;

Here we have created a generic hash-like object and set on it different values and functions. When we define class by defining a constructor and filling in the prototype object, we simply give an instruction about how the object should initially be wired up. The functions that are defined on the class are not actually related in any way to the class.

Get Prototype and Scriptaculous: Taking the Pain out of JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.