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