Friday, September 9, 2022
HomeWordPress DevelopmentCourses in JavaScript - DEV Neighborhood πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Courses in JavaScript – DEV Neighborhood πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»


In programming, courses are the blueprints for objects.
Let’s create a category named Individual.

class Individual {
    //js object constructor.
    constructor() {
        this.identify = "Max";
    }

    printMyName() {
        console.log(this.identify);
    }
}
Enter fullscreen mode

Exit fullscreen mode

Above class Individual is having two issues in it is physique, a constructor operate to create and initialise occasion variable and a way β€˜printMyName’ to print the worth in occasion variable.
The above class β€˜Individual’ is a blueprint. With a view to give life to this class, we’ve to create object for this class β€˜Individual’.
Let’s create an object for the category β€˜Individual’.

const individual = new Individual();
individual.printMyName();
Enter fullscreen mode

Exit fullscreen mode

Within the above code, we’re creating an object utilizing new key phrase.
And we’re calling the tactic β€˜printMyName’ to print the worth in occasion variable.

Inheritance
Like a toddler inherits the behaviour of his/her dad and mom, we will additionally make a category inherits the strategies and properties of one other class.
Let’s create a inherited class,

class Human {
    constructor(){
        this.gender="male";
    }

    printGender() {
        console.log(this.gender);
    }
}

class Individual extends Human {
    //js object constructor.
    constructor() {
        tremendous();
        this.identify = "Max";

    }

    printMyName() {
        console.log(this.identify);
    }
}
Enter fullscreen mode

Exit fullscreen mode

Right here we use the key phrase extends to make the category β€˜Individual’ inherits the strategies and attributes of the category β€˜Human’.
Additionally we use tremendous() inside constructor operate as a result of, we’re overriding the constructor operate at school β€˜Individual’ and due to that the constructor operate at school β€˜Human’ will not have any impact. The tremendous() operate is used to present entry to strategies and properties of a mum or dad or sibling class.
Now let’s create object from this inherited class,

const individual = new Individual();
individual.printGender();
Enter fullscreen mode

Exit fullscreen mode

Since we’re inheriting the category β€˜Human’ at school β€˜Individual’, we will entry the tactic printGender() of β€˜Human’ class from the category β€˜Individual’.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments