I’m going to compare only ES5 classes vs ES6 classes, as that is one of the major differences that has drawbacks. Other differences are language features that improve over ES5, and in my opinion, only ES6+ classes have real downsides.
You have a lot of browser support.
Classes and OO inheritance. (here’s a list of 41 libraries for doing OOP in ES5, and I didn’t even bother to continue my search on NPM after page 6 of searching for “class inheritance”: #587 · w3c/web components)
You can easily make “interfaces”, “multiple inheritances”, “protected and private members”, and other inheritance features in ES5 because it is flexible. See the first 11 libraries that I linked, which have interesting features.
You can use `this` before calling a super constructor in an ES5 class.
You can dynamically modify class prototypes to achieve certain things. For example, there are valid tricks whereby you temporarily remove a prototype, do something, then add it back.
You can inject prototypes in between prototypes.
ES6 classes (class syntax, which isn’t purely sugar):
Classes and OO inheritance.
“syntax sugar”, literally that’s all, but it’s not completely just syntax sugar, read the cons.
In some cases, builtin classes can only be extended with native ES6 `class` syntax.
It doesn’t have the “syntax shortcut” that ES6 has, but there’s plenty of libraries that help with that, and the flexibility lets you create tools to do it how you want.
It doesn’t have all the support that ES5 has, but you can always transpile your ES6 code.
You can not use `this` before calling a super constructor in an ES6 class.
You can not modify class prototypes, they are configurable false and writable false.
You can not inject prototypes between prototypes.
Using POJOs to define classes in an inheritance library is complicated because simple things like applying or calling a super constructor become impossible due to ES6 limitations:
[[HomeObject]] and `super` are static, so when you use concise method shorthand in a POJO,
that function can no longer be called as a constructor with the `new` keyword,
it’s `prototype` property can no longer be modified,
and if you remove it from the object and assign it to another object, you will not get the results that you expect.
The things that most of those above 41 libraries do will simply not work with ES6 features (which is a bummer). All in all, ES6 classes are not just sugar for ES5 classes, ES6 classes are sugar with a bunch of limitations and restrictions. You can’t simply modify an ES6 class dynamically like you can an ES5 class. As far as functionality goes, the only reason you need an ES6 class is if you want to extend certain builtin classes (`Array` for example), otherwise, ES5 classes let you achieve more, just without syntax sugar, but those above libraries give you some form of “candy” that makes it nicer and more semantic.
For more information click here
Created on Jan 1st 2019 04:56. Viewed 44 times.
No comment, be the first to comment.