Stage 5: Jumping into JavaScript
Classes, Methods and Inheritance

In JavaScript, classes are a special type of function that provides a convenient way to create objects with a similar structure. Classes were introduced in ECMAScript 6 (ES6) as a way to bring object-oriented programming concepts to JavaScript.

Here is a simple example of a class in JavaScript:

 
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
 
  sayHello() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}
 
const person = new Person('John Doe', 30);
person.sayHello(); // Hello, my name is John Doe and I am 30 years old.
 

In this example, the Person class has a constructor that takes two arguments, name and age, and sets them as properties on the this object. It also has a method sayHello that logs a greeting to the console. To create a new instance of the Person class, we use the new operator and pass in the desired values for name and age.

  • In addition to constructor and methods, classes in JavaScript also support inheritance and polymorphism. This allows you to create complex and flexible object-oriented structures in your code.

  • Classes, methods, and inheritance are fundamental concepts in object-oriented programming, and JavaScript supports all of these concepts with its class syntax. Here's an example that demonstrates classes, methods, and inheritance in JavaScript:

    class Shape {
      constructor(width, height) {
        this.width = width;
        this.height = height;
      }
     
      getArea() {
        return this.width * this.height;
      }
    }
     
    class Rectangle extends Shape {
      constructor(width, height) {
        super(width, height);
      }
     
      getPerimeter() {
        return 2 * (this.width + this.height);
      }
    }
     
    const rectangle = new Rectangle(10, 5);
    console.log(rectangle.getArea()); // 50
    console.log(rectangle.getPerimeter()); // 30

    In this example, we have a Shape class with a width and height property, as well as a getArea method that calculates the area of the shape. We then have a Rectangle class that extends the Shape class. The Rectangle class has its own constructor that simply calls the super method to inherit the properties from the Shape class. The Rectangle class also has a getPerimeter method that calculates the perimeter of the rectangle. To use the Rectangle class, we create a new instance of it using the new operator and passing in the desired width and height values. We can then access the getArea and getPerimeter methods on the rectangle object to calculate the area and perimeter of the rectangle.

This example demonstrates how you can use classes, methods, and inheritance in JavaScript to create reusable and flexible object-oriented structures in your code.