Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a class in JS OOP

Can You help with this thing that I need to figure out. I started learning Js with OOP but I am kind of stuck with this, where am I making a mistake. This is the assignment I have to figure out

Create a class Car with a property that holds a number of doors and one method that prints the number of doors to the console.

class Car { 
    constructor(doors){
     this.doors=doors
     console.log(doors)
    }
}
like image 763
whatever Avatar asked Mar 01 '23 12:03

whatever


1 Answers

you need to create a method in the Car class to print the number of doors and then you need to instantiate the class with a given number of door & then call that method on it.

class Car { 
    constructor(doors){
     this.doors = doors;
    }
    print(){
        console.log(this.doors);
    }
}

const bmw = new Car(4);
bmw.print()
like image 111
Sina Avatar answered Mar 07 '23 13:03

Sina