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

How to iterate array elements using 'for loop' in JavaScript