JavaScript classes provide a modern way to handle object-oriented programming (OOP) concepts such as inheritance, encapsulation, and polymorphism. In this guide, we will explore how to create classes, how inheritance works in JavaScript, and how to extend classes to create more complex objects.
In ES6, JavaScript introduced a cleaner and more intuitive syntax for creating objects using the class keyword.
class ClassName { constructor() { // Initialization code } methodName() { // Method code } }
class Animal { constructor(name, type) { this.name = name; this.type = type; } speak() { console.log(`${this.name} makes a sound.`); } } const dog = new Animal('Buddy', 'Dog'); dog.speak(); // Output: Buddy makes a sound.
Inheritance allows one class to inherit the properties and methods from another class. In JavaScript, this can be achieved using the extends keyword.
class ChildClass extends ParentClass { constructor() { super(); // Calls the parent class constructor // Additional initialization code for child class } }
class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a sound.`); } } class Dog extends Animal { constructor(name, breed) { super(name); // Call the parent class constructor this.breed = breed; } speak() { console.log(`${this.name}, the ${this.breed}, barks.`); } } const dog = new Dog('Buddy', 'Golden Retriever'); dog.speak(); // Output: Buddy, the Golden Retriever, barks.
In JavaScript, when a child class overrides a method of the parent class, the child class version of the method is used. This is known as method overriding.
class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a sound.`); } } class Cat extends Animal { speak() { console.log(`${this.name} meows.`); } } const cat = new Cat('Whiskers'); cat.speak(); // Output: Whiskers meows.
JavaScript does not directly support multiple inheritance, meaning a class cannot inherit from multiple classes simultaneously. However, you can work around this limitation by using mixins.
class ClassName { constructor() { // Initialization code } methodName() { // Method code } }
Static methods and properties belong to the class itself rather than instances of the class. They are called directly on the class.
class Animal { constructor(name, type) { this.name = name; this.type = type; } speak() { console.log(`${this.name} makes a sound.`); } } const dog = new Animal('Buddy', 'Dog'); dog.speak(); // Output: Buddy makes a sound.
Getters and setters allow you to define special methods for getting and setting object properties. These are commonly used for encapsulating the state of an object.
class ChildClass extends ParentClass { constructor() { super(); // Calls the parent class constructor // Additional initialization code for child class } }
Classes and inheritance are essential concepts in object-oriented programming, and understanding them will help you write cleaner, more maintainable JavaScript code.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
The above is the detailed content of Understanding Classes and Inheritance in JavaScript. For more information, please follow other related articles on the PHP Chinese website!