O'Reilly logo

Prototype and Scriptaculous: Taking the Pain out of JavaScript by Chris Angus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required