Is “Class” In ES6 The New “Bad” Part?
rajaraodv
20120

I think classes are a “bad part” in ES2015. I think class in Javascript is syntactic sugar that provides a poor and misleading abstraction to developers, and isn’t necessary for good object-oriented programming. It’s a continuation of the error made early in Javascript’s design: trying to make the language look like something it’s not in an effort to make some developers more comfortable (“look isn’t this familiar…you already know this…”). And I also think you don’t need classes to write object-oriented code in Javascript (actually, you don’t need prototypes, either, but that’s a tangent).

The ES2015 specification, itself, recognizes the disconnect. It says “Even though ECMAScript includes syntax for class definitions, ECMAScript objects are not fundamentally class-based such as those in C++, Smalltalk, or Java…In a class-based object-oriented language, in general, state is carried by instances, methods are carried by classes, and inheritance is only of structure and behaviour. In EMCAScript, the state and methods are carried by objects, while structure, behaviour, and state are all inherited…Although ECMAScript objects are not inherently class-based, it is often convenient to define class-like abstractions…”

I think it is only “convenient” if one wants to write code using class-based concepts and constructs (i.e., wants Javascript to be more like some other language, when it’s really not). And I think it’s a “convenience” that is similar to the “convenience” of semicolon insertion: might seem harmless enough on the surface, but introduces subtle problems/complications that will become more apparent down the line. Abstraction is often good, if the abstraction works for the concept. But class in Javascript is a bad abstraction: it masks the underlying nature of the language and misleads developers who might be familiar with class-based languages while being new to Javascript. (Similar to how function-scoped vars mislead such developers.) Javascript’s class is a cat costume on a dog: might look like a cat, but you’re in for a surprise when you expect it to use the litter box (you can play fetch with it, though).

I think Javascript developers should learn and embrace the actual nature (and strengths) of the language, rather than trying to dress it up as something it’s not. We’ll all write better Javascript that way. Unfortunately, we’re now stuck with class in Javascript.