05. JavaScript Classes: Defining Classes, Inheritance & Static Methods
Posted on 2025-10-12 17:15:41
1
Price: $0.00
Quota Left: 0
Introduction
Classes in JavaScript provide a cleaner, more expressive syntax for object-oriented programming. They let you define blueprints for objects, support inheritance, and define static (class-level) methods. In this post you will learn:
- How to define a class
- How to use inheritance with extends
- How to use and define static methods and properties
1. Defining a Class
You can define a class in JavaScript using the class keyword. A class encapsulates a constructor and methods.
Syntax:
- constructor(...) is a special method used when creating a new instance
- Methods defined in the class (like greet) become part of the prototype and are shared across instances
- You instantiate with new:
2. Class Inheritance
Inheritance allows one class to extend another, inheriting its methods and properties, while adding or overriding functionality.
Syntax:
- extends sets up the prototype chain
- super(...) calls the parent class constructor (must be used before accessing this)
- Subclasses inherit methods from the parent:
You can also override parent methods:
Here super.greet() calls the parent’s greet method within the child.
3. Static Methods & Properties
Static methods are defined on the class itself, not on instances. They’re useful for utility operations that don’t depend on instance data.
Definition:
- Call static methods via the class, not an instance:
- Static methods cannot access this in the context of an instance—they’re detached from instances
- Static methods are often used for factory methods, helper functions, or operations on class-wide data
You can also define static properties using the static keyword (supported in newer JavaScript versions).
Conclusion
JavaScript’s class syntax provides a more intuitive way to write object-oriented code. With classes, you can:
- Define reusable blueprints (constructors and methods)
- Extend behavior via inheritance
- Use static methods and properties for class-level logic