JavaScript Classes

Subject: JavaScript

JavaScript classes provide a clear and structured way to create objects and handle inheritance. Introduced in ES6, class syntax is syntactical sugar over prototype-based inheritance.


Syntax of a JavaScript Class


Example 1: Creating a Class and an Object

  • The constructor() method runs when a new object is created.
  • this refers to the current object instance.

Example 2: Adding Multiple Methods


Inheritance Using extends

Create a subclass that inherits from a parent class.

Example 3: Inheritance


Using super Keyword

Use super() to call the parent class constructor or methods.


Static Methods

Static methods belong to the class itself, not instances.


Key Takeaways

  • Classes are blueprints for creating objects.
  • Use constructor() for initialization.
  • Define methods inside the class body.
  • Use extends for inheritance.
  • Use super() to call parent constructor or methods.
  • Static methods belong to the class, not instances.
Next : Class Inheritance