| constructor function | Prototypes & Inheritance | |
๐ ES6 Classes in JavaScript |
ES6 (ECMAScript 2015) introduced class syntax to provide a cleaner and more familiar way for developers to create objects and handle inheritance. While they look like traditional classes from languages like Java or C++, they are actually "syntactic sugar" over JavaScript's existing prototype-based inheritance model.
The core components of a class are the class keyword, a constructor() method, and instance methods.
// A simple class declaration
class Vehicle {
// A special method for creating and initializing objects
constructor(make, model) {
this.make = make;
this.model = model;
}
// A method that can be called on an instance of the class
displayDetails() {
console.log(`This is a ${this.make} ${this.model}.`);
}
}
// Create new objects (instances) using the `new` keyword
const myCar = new Vehicle("Toyota", "Camry");
const myTruck = new Vehicle("Ford", "F-150");
myCar.displayDetails(); // Output: "This is a Toyota Camry."
myTruck.displayDetails(); // Output: "This is a Ford F-150."
extends and superOne of the most powerful features of ES6 classes is the ability to create subclasses that inherit from a parent class.
class Car extends Vehicle {
constructor(make, model, year) {
super(make, model); // Call the parent constructor
this.year = year;
}
displayDetails() {
// Override the parent method and add new behavior
console.log(`This is a ${this.year} ${this.make} ${this.model}.`);
}
}
const myNewCar = new Car("Honda", "Accord", 2025);
myNewCar.displayDetails(); // Output: "This is a 2025 Honda Accord."
class and extends keywords provide a more intuitive and readable syntax compared to the older prototype-based inheritance.# prefix) allows for better data hiding and encapsulation.new keyword throws an error, preventing a common mistake found with older constructor functions.{ key: "value" }) or a factory function is often simpler and more appropriate.class Vehicle).super() before accessing or setting properties on this.# prefix for private fields to properly encapsulate data that should not be accessed from outside the class.static keyword for helper methods that belong to the class itself rather than to an instance.this context will change. Use arrow functions or .bind() to ensure the correct context.This is the traditional way to create objects before ES6 introduced the class keyword.
// Constructor function
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
// Adding a method using prototype
Car.prototype.drive = function() {
console.log(`Driving the ${this.make} ${this.model}`);
};
// Creating instances
const car1 = new Car("Toyota", "Camry", 2024);
car1.drive();
This is the modern, cleaner syntax introduced in ES6. It uses the same prototypal inheritance under the hood.
// ES6 class
class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
drive() {
console.log(`Driving the ${this.make} ${this.model}`);
}
}
// Creating instances
const car2 = new Car("Honda", "Accord", 2025);
car2.drive();
new; constructor functions do not.extends and super() for inheritance.For modern development, prefer ES6 classes for better readability, maintainability, and built-in inheritance support.
| constructor function | Prototypes & Inheritance | |