DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Is ''Class'' in ES6 the New ''Bad'' Part?

Is ''Class'' in ES6 the New ''Bad'' Part?

So, class in ES6... some like it, others don't. Let's go over some of the pros and cons, and you can make up your own opinion about it.

Raja Rao user avatar by
Raja Rao
·
Aug. 22, 16 · Opinion
Like (10)
Save
Tweet
Share
8.23K Views

Join the DZone community and get the full member experience.

Join For Free

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 that the “Class”-syntax in ES6 has gotten better and more 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 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 for a better understanding.

Argument: Class In ES6—The "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 from the beginning. However, there was no “Class” keyword and the code to set it up, create sub classes, and call parent’s functions was weird and confusing.

Also, most JS folks just want to do basic OO stuff and move on. But, the current syntax throws 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 the real-world analogy of “Living things” (like humans) versus “Nonliving things” (like cars, furniture, etc.).

The below picture depicts how object creation differs in Java from that of JavaScript.






Objects in Java World vs. JavaScript World


As you can imagine, in JS, there is no overhead or 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 thing because:

  1. Class is something everyone learns and making the syntax better is a good thing.
  2. It’s an optional feature and there are other ways to create objects like factory functions.
  3. Using it for limited purposes is fine.

It's a bad thing because:

  1. The concept of “Class” doesn’t exist in JavaScript.
  2. Concept of classes makes things brittle. Prototypes are better and very flexible.
  3. It guides people away from the goodness and power of functional programming.

So, where do you stand? Is “Class” in ES6 the new “bad” part?

That’s it!

Object (computer science)

Published at DZone with permission of Raja Rao, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Web Application Architecture: The Latest Guide
  • The Future of Cloud Engineering Evolves
  • Java Development Trends 2023
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: