DEV Community

Albert
Albert

Posted on • Updated on

Multi-Constructor on JavaScript

Hope you like it!

Hi guys i would like to share with you my point of view how i define multi-constructors on Javascript.

class Car {
  constructor(brand, year = '', owner = '') { // assign default value
    this.carname = brand;
    this.year = year;
    this.owner = owner;
  }
  presentCarName() {
    return 'I have a ' + this.carname;
  }
  presentCarNameAndYear() {
    return 'I have a ' + this.carname + ' year: ' + this.year;
  }
}

let myCar = new Car("Ford");
console.log(myCar.presentCarName());
myCar = new Car("Ford", 1996);
console.log(myCar.presentCarNameAndYear());
Enter fullscreen mode Exit fullscreen mode

on stackoverflow

hope you like it!

Top comments (0)