Guidelines
Classes

Classes

Avoid classes

Avoiding classes can improve the simplicity and clarity of the code, as it reduces the need for complex inheritance hierarchies and encourages the use of simple functions and data structures.

It can also make the code more portable and easier to test and maintain, as it avoids the need to deal with the implementation details of classes and their internal state.

Use class syntax

The class syntax was introduced in ECMAScript 2015 (ES6), which is a version of the JavaScript language that was released in June 2015. Prior to the introduction of the class syntax, JavaScript had a prototype-based object-oriented model, which means that classes had to be simulated using functions and objects.

The ES6 class syntax provides a more familiar and intuitive syntax for defining object-oriented code, which can make it easier to learn and work with for developers who are familiar with other object-oriented languages. It also promotes encapsulation and separation of concerns, which can help to organize and maintain the code. The class syntax also makes it easier to work with inheritance and polymorphism, as it provides a clear and concise syntax for defining subclass relationships and method overrides.

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
 
  greet() {
    console.log(
      `Hello, my name is ${this.name} and I am ${this.age} years old.`
    );
  }
}
 
const person1 = new Person("John", 30);
person1.greet(); // Output: "Hello, my name is John and I am 30 years old."
Last updated on