JavaScript intro to classes

Classes are objects 'functions which introduce inheritance parent child relationship in JavaScript it promotes code reusability which reduces the amount of code in a JavaScript application.
JavaScript class decelerations start with keyword class proceeded by class name with a class body having a constructor along with methods for instance

class human {
    constructor(name) {
         this.name = name;
         this.gender = null;
    }
    great() {
        return "Greetings " +
        (this.gender == "male") ? "mr " : "mrs " + this.name
    }
}
Child classes are created by adding the keyword extends followed by the parent class name and parent class methods can be called using the syntax super.+"method name"(parameters)
class man extends human {
    constructor(name) {
         super(name)
         this.gender = "male";
    }
}
const John = new man("John swana")
John. greet()
// returns Greetings mr John Swana

Comments

Popular posts from this blog

Install steam locomotive sl using apt

Tar basic commands to store and extract files

Rest operator in JavaScript

JavaScript es6 template literals

how to dump a postgresql database using from the terminal