create a mother in javascript

In JavaScript, we can create a 'mother' class or parent class using the class keyword. We can then define properties and methods in this class that can be inherited by the child classes.

Here is an example of how to create a mother class in JavaScript:

index.tsx
class Mother {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  sayName() {
    console.log(`My name is ${this.name}.`);
  }

  sayAge() {
    console.log(`I am ${this.age} years old.`);
  }
}
220 chars
15 lines

In this example, the Mother class has two properties - name and age, and two methods - sayName and sayAge.

To create a child class that inherits from the Mother class, we can use the extends keyword. Here is an example:

index.tsx
class Child extends Mother {
  constructor(name, age, hobby) {
    super(name, age);
    this.hobby = hobby;
  }

  sayHobby() {
    console.log(`My hobby is ${this.hobby}.`);
  }
}
182 chars
11 lines

This Child class inherits all of the properties and methods from the Mother class, and also has its own property - hobby - and method - sayHobby.

We can create an instance of the Child class and call all of its methods like this:

index.tsx
const child = new Child('Alice', 10, 'reading');
child.sayName(); // My name is Alice.
child.sayAge(); // I am 10 years old.
child.sayHobby(); // My hobby is reading.
167 chars
5 lines

This example demonstrates how we can create a 'mother' or parent class in JavaScript and inherit its properties and methods in the child classes using the extends keyword.

gistlibby LogSnag