Stop classifying JavaScript

Recommendations

Note: Everyone has their own programming style and by no means should people be forced to obey the styles of others. However, I do believe that best practices should be established for any language based on the strengths and weaknesses of that language. If you are using classical inheritance patterns in JS, I’m not saying that you absolutely must let go of them and adopt the style I recommend, but I think that everyone should give this style at least two tries.

If there is one thing about many JavaScript developers that makes me aggravated, it’s that they won’t let go of their classical inheritance patterns.

The rise of CoffeeScript and TypeScript are symptoms of this problem.

TypeScript isn’t all bad. At least it is a superset, so existing JS can be copy and pasted right in, no problem. It has static type checking, which can be nice in large code bases.

CoffeeScript, however, is an entirely different story. Without learning and coding in the language, it is hard to read, even when compiled into JS.

The two languages share what seems to be a common goal: bring classical inheritance patterns into JavaScript. This seems to be a pretty popular thing among devs, and it kind of pisses me off.

Say it with me:

JavaScript is not a classical language

It’s prototypal, so use prototypes. You should be defining prototypes with objects and constructing objects with factories and Object.create like this:

var MyClass = {
prototype: {
// prototypal members and methods
},
create: function(options){
// do stuff with options
return Object.create(MyClass.prototype, options);
}
};

Not with the terrible class definition syntax introduced with ES6 and not with any other compile-to-JS language.

This is, in my opinion, cleaner and clearer than classes, and also fits the architecture of the language more closely. It helps play into the functional strengths of the language.

I understand attraction to classical inheritance. Heck, even Douglas Crockford implemented pseudo-classical inheritance until he was enlightened, and the creator of JavaScript, Brendan Eich, seems to like ES6 classes. For many programmers, it is all they ever knew before JavaScript, and familiarity is comfortable. However, JavaScript is not a classical language, and syntactic sugar like ES6 classes and the new keyword just confuse people.

We should be teaching people how prototypal inheritance works, why it’s awesome, and the best practices of using it. We shouldn’t be pretending that this language is like C++ or Java.

I think people like Eric Elliott and Douglas Crockford share these concerns, and we need to make them heard.

For more on why the new keyword is terrible, why prototypal inheritance is better than classical inheritance, etc, check out The Two Pillars of JavaScript by Eric Elliott.