02
Objects Without Classes
Eich chose Self-style delegation as early as May 1995 and expected it to come out even faster than classes: one link to a prototype, and through it the object gets everything it cannot do itself. He did not manage to expose that link to the programmer in ten days. In JavaScript 1.0, which shipped in March 1996 with Navigator 2.0, there was no way to ask an object to inherit anything from another object.
How people lived under those conditions can be seen in an example from the history of the language written by Allen Wirfs-Brock and Eich himself. Shared methods were handed out to every instance by hand, right in the constructor:
function ptSum(pt2) { return new Point(this.x + pt2.x, this.y + pt2.y); }
function Point(x, y) {
this.x = x;
this.y = y;
this.sum = ptSum; // every point gets its own reference to the method
}
There is one function ptSum, but a reference to it has to be handed to every point. If tomorrow you need a distance method, you have to dig into the constructor, and points created before that will live out their lives without it. For half-screen scripts this was enough; nobody wrote anything longer in JavaScript back then.
In 1996, together with Navigator 3, came JavaScript 1.1. Every function got a prototype property, and the Self mechanism finally surfaced. Methods were now put on the prototype once, and points created with new found them on their own:
function Point(x, y) { this.x = x; this.y = y; }
Point.prototype.sum = ptSum;
Point.prototype.distance = ptDistance;
var origin = new Point(0, 0);
The documentation greeted the novelty without fanfare. The JavaScript 1.1 guide described prototype as «a property that is shared by all objects of the specified type», and a quarter of a century later Wirfs-Brock and Eich politely call that phrase vague. The vagueness was political. Netscape still stood by the press-release version of 1995: objects, meaning classes, are written in Java, and JavaScript is there to glue those objects together. In that picture of the world, JavaScript’s own ways of describing objects looked superfluous, and they were barely documented. So the mechanism on which all objects of the language would later be built appeared almost unnoticed.
From the outside, a prototype is easy to mistake for a C++ virtual method table, where the constructor works like a class and the points share common methods. As long as the program behaves, that reading works. As soon as it starts using the freedom Self gave it, the reading breaks, because a prototype is a live link. An object follows it every time it does not find a property on itself, and anything that appears on the prototype is immediately visible to all its descendants, including those born earlier:
Free, one step away
The rest of the book is free
Leave your email and I will send a link to confirm it. Once you click it, every chapter opens here, on this device, for a year. When a new book comes out I will write to this address; reply “stop” to any email and I will not write again.
Have an INITE account? Sign in and the book opens without an email.
I do not charge for the book. If you would like to support it, you can donate from any chapter.
How your email is stored