Class Constructor Example

Use a constructor to initialize object properties.

Preview

Code

<script>
class Car {
  constructor(brand, year){
    this.brand = brand;
    this.year = year;
  }
}

let car = new Car("Toyota", 2023);
console.log(car.brand, car.year);
</script>

More JS Classes Examples (7)