I’ve tried searching Hashnode, all I am finding are discussions whether or not to use ES6 classes, or what they are?
What are the actual benefits that I forego if I choose not to use the “class” syntactic sugar, and settle with ES5 classes?
I really like to use classes for interfaces in order to split up code and have a clean code base. Go figure in one of my projects. The actual implementation goes someplace else, usually other files.
Example:
// my-class.js
module.exports = class MyClass {
/**
* Lengthy description
*/
foo() { throw 'Not Implemented: foo'; };
// more of that
};
// in some other file:
require('./my-class.js').prototype.foo = function() {
// actually do something useful here!
};
Also, creating real mixins for object composition is super easy with the class sugar!
Example:
class MyClass extends mix(MyBaseClass).with(Mixin1, Mixin2) {
// implementation or interface or w/e
};
First, look at the transpiled code. Transpilation is not free, and ES5 is directly supported by many browsers.
Mev-Rael
Executive Product Leader & Mentor for High-End Influencers and Brands @ mevrael.com
Never.
ES6 "Classes" don't give you anything and their implementation is also very weak. You have to put super(), you can't define properties and you still have to use Object.prototype when you need to do something more specific.
There are no "classes" in ES5 either. Just use plain objects.
Object.create(),Object.assign()Keep your code simple and write your code same way and not in different ways. I am not using classes and even prototypes anymore. Just plain objects which operates on data or DOM, that's the simplest approach I found. Example:
const MyString = { concat(str1, str2) { return str1 + str2; } } const MyButton = { getAllButtons() { return document.getElementsByTagName('button') || false; } init(button) { button.addEventListener('click', () => { console.log('My Button clicked'); } } }