Why You Should Not Use Classes in JavaScript

Why You Should Not Use Classes in JavaScript

When I am writing JavaScript objects I prefer to use factory functions instead of classes. This article explores the reason why you should not use Classes in JavaScript.

I recently wrote a post  on the way I like to create objects in JavaScript. I call this style of object creation ‘Crockford Objects’, named after Douglas Crockford, author of “JavaScript: The Good Parts ”.

I received a lot of feedback from people telling me that I did not explain why they should not use the class keyword in JavaScript. I didn’t explain why because I was not saying that you can’t use it, I just prefer not to use the class keyword for defining an object.

JavaScript is not an Object-Oriented Language

If you are familiar with the history of JavaScript, Brendan Eich was told that he was going to be able to write a scripting language for the Netscape Navigator to be based on Scheme. Scheme is a functional programming language. Then Eich was told that it had to look similar to Java, hence the name ‘JavaScript’. Oh, by the way, he only had two weeks to get it done.

So JavaScript was never intended to be an object-oriented language. It is actually a prototype-oriented language.

Douglas Crockford likes to joke that most developers who start using JavaScript never bother to actually learn the language first. And if you start looking at the language it has a lot of similiarities to Java and C#. In Java and C## you define classes as a template for a new object. In JavaScript, everything is an object.

Classes were added to JavaScript as a concession for C## and Java developers. Underneath the hood, the JavaScript engines still create objects the same way.

javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Data Types In JavaScript

JavaScript data types are kept easy. While JavaScript data types are mostly similar to other programming languages; some of its data types can be unique. Here, we’ll outline the data types of JavaScript.

Introduction With Basic JavaScript

Introduction With Basic JavaScript - Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world.