In 1995, Brendan Eich was hired by Netscape and asked to create a language for their HTML browser. Rumors say, he designed Mocha in 10 days, later renamed to LiveScript, and then to JavaScript. It was planned to make it similar to Scheme, a LISP-syntax language. Instead, to please the crowd of C++/Java coders, it was made syntactically similar to Java. In 2008, Brendan made a tragic mistake: he donated $1,000 in support of Californian anti-gay marriage law. In 2014, he joined Mozilla as a CEO and the crowd remembered his anti-diversity gesture. He had to step down and founded Brave Software, the developer of the Brave browser. Somewhere around that time they started to kill JavaScript. Still doing it pretty good, thanks to recent ECMAScript updates and TypeScript.
In the JavaScript created 30 years ago, objects were primitive associative arrays of properties. Either data, a function, or another object may be attached to a property of an object. Letâs see what Brendan said about JS in 2008:
Iâm happy that I chose Scheme-ish first-class functions and Self-ish prototypes as the main ingredients.
First-class functions mean that itâs possible to use a function as a value assignable to a variable. And then he concluded (pay attention, itâs C not C++):
Yet many curse it, including me. I still think of it as a quickie love-child of C and Self.
Self, which he refers to a few times, is a prototype-based object-oriented language designed by David Ungar and Randall Smith in Xerox PARC, then Stanford University, and then Sun Microsystems. Self doesnât have classes, unlike Java or C++. Instead, it only has objects. To create a new object in Self we make a copy of an existing object, known as prototype, and then modify some of its slots (attributes).
In Self, objects donât have types: all method calls are dispatched in runtime. For example, we ask a book to rename itself:
book rename: "Object Thinking".
The rename is the method of the book that we call with a single string argument. The computer doesnât know anything about the book until itâs time to call the rename method. Obviously, such a duck typing has its performance drawback. Every rename leads to a search in a virtual table of book. To the contrary, C++, where types are known in compile time, can dispatch rename() instantly:
Book b;
b.rename("Object Thinking");
Types (classes in C++ and interfaces in Java) and type annotations are helpfulâto the compiler. To us humans they are a burden. They require us to do the work of the compiler. We have to pollute our code with messages like: âThis object b is of type Book, please remember.â The compiler must be smart enough to understand it without our hints.
This is a debatable topic though. Some believe that type annotations help programmers better understand the code and make fewer mistakes. Iâm also in favor of fewer mistakes, but would rather expect the compiler to infer types automatically, without my annotations. If I do b.rename() and b is known to be a car instead of a book, I would expect the compiler to figure this out on its own and refuse to compile.
Anyway, JavaScript was designed as a prototype-based dynamically typed language with a minimalistic syntax that resembles Java. It worked perfectly fine until the industry decided to âfixâ it.
In 2008, Mozilla and others proposed ECMAScript 4, which included classes, modules, and other features. Microsoft took an extreme position, refusing to accept any part of ES4. Chris Wilson, Microsoftâs Internet Explorer platform architect, criticized ES4 for trying to introduce too many changes. Brendan Eich accused Wilson of spreading falsehoods and playing political games. ES4 was abandoned, and classes were dropped.
Then, in 2012, Microsoft created TypeScript, a JavaScript with type annotations and classes. Since classes werenât in the standard, Microsoft made their own.
Finally, in 2015, ECMAScript 6 added classes (among other features) to the JavaScript specification. Many ES4 features, including classes, were revived in a âmaximally minimalâ form. The crowd of Java/C++ developers got what they wanted.
Letâs hear what Douglas Crockford, one of the evangelists of JS, said in 2014:
Class-free programming is JavaScriptâs contribution to humanity.
Unfortunately, not anymore. It looks like the developers of recent versions of JS believe in something else. Or maybe they donât want to make a contribution to humanity anymore. Maybe they just want to make the crowd happy.
Type annotations and classes donât match with the concept of class-free object-based programming of JavaScript. They came from Java or C++ but donât fit in. Some programmers may find them helpful, but only because they are used to seeing them in other languages. JavaScript is not Java, even though the names look similar.
Java, with its classes, implementation inheritance, and static methods, is OOP for dummies. Itâs object-oriented programming for those who, according to Douglas Crockford, donât know what object-oriented programming is. JavaScript, on the other hand, is a member of conceptually solid OO languages, like Smalltalk and Self. Blending Java flavors into JavaScript only ruins the flavor of the latter.
Itâs sad to see how a once straight object-centric language paradigm turned into a diversity of unmatchable and suboptimal features.
P.S. JavaScript is still a great language if you ignore classes and type annotations. When I write in JavaScript, I donât use them. Look at the code in the yegor256/jo repository. It illustrates the Junior Objects book of mine. Iâm proud of this code.
