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:
Gratis, solo falta un paso
El resto del libro es gratis
Deja tu correo y te mando un enlace para confirmarlo. Después del clic, todos los capítulos se abren aquí, en este dispositivo, durante un año. Cuando salga un libro nuevo, escribiré a esa dirección; responde “basta” a cualquier correo y no vuelvo a escribir.
¿Tienes cuenta INITE? Entra y el libro se abre sin correo.
No se cobra nada por el libro. Si quieres apoyarlo, puedes donar desde cualquier capítulo.
Cómo se guarda la dirección