Skip to main content

JavaScript Class

JavaScript classes are a way to define a blueprint for creating objects that share the same properties and methods.

They were introduced in ECMAScript 2015 (ES6) and are a syntactical sugar over the prototype-based inheritance model in JavaScript.

To create a class in JavaScript, you can use the class keyword followed by the name of the class.

As an example:

class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}

drive() {
console.log(`Driving a ${this.make} ${this.model}`);
}
}

In this example:

  • We have defined a class called Car with a constructor method that accepts two parameters (make and model) and initializes two properties (this.make and this.model) with their values.
  • We have also defined a drive method that logs a message to the console.

To create an object from this class, we can use the new keyword and call the constructor with the appropriate arguments:

const myCar = new Car("Honda", "Civic");
myCar.drive(); // Output: Driving a Honda Civic

JavaScript Class Syntax

JavaScript classes are defined using the class keyword followed by the name of the class. The body of the class is contained in curly braces and consists of a constructor method and zero or more methods and properties.

Here is the syntax for a basic class definition:

class MyClass {
constructor() {
// constructor code here
}

method1() {
// method code here
}

method2() {
// method code here
}

// more methods and properties here...
}

Explanation:

  • The constructor method: This is a special method that is called when an object of the class is created. It is responsible for initializing the object's properties and setting its initial state. In this example, the constructor method is empty, meaning that it does not initialize any properties.

  • method1 and method2: These are methods of the class that can be called on instances of the class. They define the behavior of the class and allow you to perform actions on objects created from the class.

  • Comment indicating that more methods and properties can be added: This is a placeholder for additional methods and properties that can be added to the class.

Why javascript classes

They help to organize code and promote code reuse by encapsulating related data and behavior into a single unit.

Some benefits of using JavaScript classes include:

Encapsulation

Classes allow you to encapsulate related data and behavior into a single unit. This makes the code easier to understand and maintain, and reduces the likelihood of errors caused by accidentally modifying data or behavior that should be isolated.

Inheritance

Classes can be used to create hierarchies of objects, where subclasses inherit properties and methods from their parent class. This allows you to reuse code and avoid duplicating logic across multiple objects.

Polymorphism

Classes can be used to create objects that share a common interface, but have different implementations. This allows you to write code that can operate on a variety of objects, without needing to know their specific implementation details.

Code organization

Classes provide a clear structure for organizing code, making it easier to read, understand, and maintain.