Show List

JavaScript Classes

JavaScript classes are template for creating objects and contain data and functions. Here is an example of a class and creating object using it.

      class Person {
        constructor(name) {
          this.name = name;
        }
        getName() {
          return this.name;
        }
      }
      let john = new Person("John Miller");

Class Inheritance

One class can inherit the data and functions from another class using inheritance. Here is an example:
      // parent class
      class Animal {
        constructor(name) {
          this.name = name;
        }

        run() {
          console.log("Running");
        }
      }

      // inheriting parent class
      class Dog extends Animal {
        bark() {
          console.log("Barking");
        }
      }

      let dog = new Dog('Karma');
      dog.run();

    Leave a Comment


  • captcha text