Why I Still Don’t Like Classes in JavaScript

Why I Still Don’t Like Classes in JavaScript

Why I Still Don’t Like Classes in JavaScript. I wrote an article pointing out the reasons why I didn’t like classes in JavaScript and I preferred using factory functions. I can still say that I don’t like classes in JavaScrip. The this parameter, privacy with classes, and factory functions are just a few pitfalls.

The this parameter, privacy with classes, and factory functions are just a few pitfalls

A few years ago, I wrote an article pointing out the reasons why I didn’t like classes in JavaScript and I  preferred using factory functions. Three years later, I can still say that I don’t like classes in JavaScript and I use them only as an exception when there are no alternatives.

The good news is that many of the class-related problems have been solved. This is great because there are still applications using them, so now there are fewer things to worry about.

Privacy With Classes

Class properties are public by default. That means we can access and modify any of the properties of an object created with a class. However, the hash (#) prefix now allows us to define private properties.

The # is a part of the property name. It is used for both declaring and accessing such a field.

Private fields are accessible only inside the class . Below is an example of the Counter  class encapsulating the count  field:

class Counter {
  #count

  constructor() {
    this.#count = 0;
  }

  increment(value){
    this.#count += 1;
    return this.#count;
  }

  decrement(){
    this.#count -= 1;
    return this.#count;
  }
}

const counter = new Counter();
counter.increment(); //1
counter.increment(); //2
counter.increment(); //3
counter.decrement(); //2

react nodejs programming javascript node

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

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

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Node.JS vs React.JS | Node.Js Vs React.Js Comparison | What to Choose

Node.JS vs React.JS: Which one to choose? See the detailed comparison between these two most popular website development frameworks.

Node-RED Module for Visual NodeJS Programming

Node-RED Module for Visual NodeJS Programming. In this article, I'm going to introduce you to a NodeJS module that allows you to create. Node-RED: A flow-based programming tool that allows you to design processes (aka flows) by wiring together microservices. Simple Node.js Express App.

10 Tricky JavaScript Snippets for Programming interviews

Job interviews are not entirely predictable, but we often still see commonly asked tricky questions. Let’s take a look at ten of snippet.10 Tricky JavaScript Snippets for Programming Interviews. Prepare yourself for that desired job program.

Visual Node.js Programming Using Node-RED

Learn how Node-RED speeds up development compared to native Node JS. I’m going to introduce you to a Node.js module that allows you to create and deploy server-side processes by using a visual, drag and drop style interface in your web browser. The module I’m referring to is called Node-RED: A flow-based programming tool that allows you to design processes (aka flows) by wiring together microservices.