Home  >  Article  >  Web Front-end  >  The Four Pillars of OOP in JavaScript

The Four Pillars of OOP in JavaScript

PHPz
PHPzOriginal
2024-07-27 19:28:23873browse

The Four Pillars of OOP in JavaScript

The Four Pillars of OOP in JavaScript

Object-Oriented Programming (OOP) in JavaScript is built on four main ideas, or "pillars." These are Encapsulation, Abstraction, Inheritance, and Polymorphism. Let's break down each one with simple examples.

Encapsulation

Encapsulation means putting together data and the functions that work on that data in one place, like a class.

Example: Car

Imagine a car. It has properties like model, color, speed, and engine. It also has functions like start and stop. We put all these variables and methods into one class called Car.

class Car {
    constructor(model, color, speed, engine) {
        this.model = model;
        this.color = color;
        this.speed = speed;
        this.engine = engine;
    }

    start() {
        console.log("Car started");
    }

    stop() {
        console.log("Car stopped");
    }
}

Abstraction

Abstraction means hiding the complex details and showing only the necessary parts.

Example: Remote Control

Think about a TV remote control. You press the "next" button to change the channel. You don't need to know how the remote sends a signal to the TV; you just use the button to change the channel.

In JavaScript, abstraction is about using simple interfaces to interact with more complex code.

class Remote {
    changeChannel() {
        console.log("Channel changed");
    }
}

let remote = new Remote();
remote.changeChannel(); // You don't need to know how this works inside

Inheritance

Inheritance means a class can inherit properties and methods from another class.

Example: Family Traits

Just like you might inherit certain behaviors or traits from your parents, a class can inherit attributes and methods from another class. When we inherit from multiple classes, it is called multiple inheritance, but JavaScript doesn't support this directly.

class Animal {
    makeSound() {
        console.log("Animal sound");
    }
}

class Dog extends Animal {
    bark() {
        console.log("Dog barks");
    }
}

let dog = new Dog();
dog.makeSound(); // Inherited from Animal class
dog.bark();      // Specific to Dog class

Polymorphism

Polymorphism means a variable, function, or object can take on multiple forms.

Example: Animal Class

Imagine an Animal class. With polymorphism, we can create a Cat class that modifies or adds new features to it.

class Animal {
    makeSound() {
        console.log("Animal sound");
    }
}

class Cat extends Animal {
    makeSound() { // Overriding method
        console.log("Meow");
    }
}

let cat = new Cat();
cat.makeSound(); // Calls the Cat's makeSound method

In summary, these four pillars of OOP—Encapsulation, Abstraction, Inheritance, and Polymorphism—help make JavaScript code organized, reusable, and easier to understand.

The above is the detailed content of The Four Pillars of OOP in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn