Yesterday I wrote a blog “5 JavaScript “Bad” Parts That Are Fixed In ES6" and mentioned “Class” as one of the fixes. Since then I’ve received a ton of responses where many thought “Class”-syntax in ES6 is better and useful. However others indicated that there was no need for the “Class” keyword because JS doesn’t have the concept of “Class” (i.e blueprint). In fact, their argument is that it may make things worse, because ES6's official “Class” is just a syntactic sugar and not technically and conceptually accurate (i.e. it’s false advertisement or cause for more confusion).
Let’s see the arguments from both sides to understand it better.
Argument: Class In ES6 A “Good” Part
The first thing people learn when they study OO language is the concept of Class and how inheritance works and so on. In JS you could simulate a “Class” and do OO programming since the beginning. However, there was no “Class” keyword and the code to set it up, create sub classes and call parent’s functions were all weird and confusing.
Also, most JS folks just want to do basic OO stuff and move on. But the current syntax throw them off.
Further, this is an optional way of creating Objects. It doesn’t prevent people from creating objects using other techniques like “function factories”, “Object literal” and so on.
So making the syntax look better and similar to other OO languages, while still keeping the “prototype” inheritance goodness is a good thing. People can use other ways if they don’t want to use “Class” to create Objects.
Argument : Class In ES6 The New “Bad” Part
1. Conceptually, there is No Class in JavaScript
Objects are created without Classes! The best way to explain this is to use a real-world analogy of “Living things” (like humans) Vs “Nonliving things” (like car, furniture).
The below picture depicts how object creation differ in Java from that of JavaScript.
You can click on the picture to zoom and read
As you can imagine, In JS, there is no overhead and constraints of needing a Class to use objects. Further “prototype”-chain based inheritance can wire up any object to any other object that may not be related (IS-A relationship). So it’s very flexible compared to Classes.
2. Bad For Functional Programming
In JS functions are first-class citizens. Functional programming is all about using functions to their fullest extent. There is a notion called: “Favor Composition over Inheritance” and here we are going in the opposite direction because “Class” notation favors “Inheritance over Composition”.
Summary
It’s A “Good” Part Because:
- Class is something everyone learns and making the syntax better is a good thing.
- It’s an optional feature and there are other ways to create objects like factory functions.
- Using it for limited purposes is fine.
It’s A “Bad” Part Because:
- The concept of “Class” doesn’t exist in JavaScript.
- Concept of classes makes things brittle. Prototypes are better and very flexible.
- It guides people away from goodness and power of functional programming.
So where do you stand? Is “Class” In ES6 The New “Bad” Part?
That’s it! 🙏
If this was useful, please click the clap 👏 button down below a few times to show your support! ⬇⬇⬇ 🙏🏼
My Other Posts
https://medium.com/@rajaraodv/latest
ECMAScript 2015+
- Check out these useful ECMAScript 2015 (ES6) tips and tricks
- 5 JavaScript “Bad” Parts That Are Fixed In ES6
- Is “Class” In ES6 The New “Bad” Part?
Terminal Improvements
- How to Jazz Up Your Terminal — A Step By Step Guide With Pictures
- Jazz Up Your “ZSH” Terminal In Seven Steps — A Visual Guide
WWW
Virtual DOM
React Performance
Functional Programming
- JavaScript Is Turing Complete — Explained
- Functional Programming In JS — With Practical Examples (Part 1)
- Functional Programming In JS — With Practical Examples (Part 2)
- Why Redux Need Reducers To Be “Pure Functions”
WebPack
- Webpack — The Confusing Parts
- Webpack & Hot Module Replacement [HMR] (under-the-hood)
- Webpack’s HMR And React-Hot-Loader — The Missing Manual
Draft.js
React And Redux :
- Step by Step Guide To Building React Redux Apps
- A Guide For Building A React Redux CRUD App (3-page app)
- Using Middlewares In React Redux Apps
- Adding A Robust Form Validation To React Redux Apps
- Securing React Redux Apps With JWT Tokens
- Handling Transactional Emails In React Redux Apps
- The Anatomy Of A React Redux App
- Why Redux Need Reducers To Be “Pure Functions”
- Two Quick Ways To Reduce React App’s Size In Production