Multi-Constructor on JavaScript
Albert

Albert @os3albert

About: I'm into shortcuts and productive features offered by IDE like VSCode and Extensions like Emmet etc...

Location:
Italy
Joined:
Jun 5, 2021

Multi-Constructor on JavaScript

Publish Date: Jun 5 '21
5 0

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!

Comments 0 total

    Add comment