-1

I want to build a Vehicle class to create objects based on it. And I want to pass an object to the constructor function as a parameter such that const vehicle = new Vehicle({vehicleType: 'car', name: 'car1', range: 500})

I have built it like below but how can I destructure the constructor parameter in order to avoid repeating 'options.' word?

class Vehicle {
  constructor(options = { vehicleType: 'car', name: '', range: '', seats: '' }) {
    this.vehicleType = options.vehicleType
    this.name = options.name
    this.range = options.range
    this.seats = options.seats
  }

  getRangeToSeatsRatio() {
    return this.range / this.seats
  }

  multiplySeatsBy(count) {
    this.seats *= count
    return this
  }

  getSeatCount() {
    return this.seats
  }

  get rangeToSeatsRatio() {
    return this.range / this.seats
  }

  
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Balalayka
  • 187
  • 2
  • 15
  • 3
    `constructor ({ vehicleType = 'car', name = '', range = '', seats = '' } = {})` – CherryDT Mar 15 '22 at 08:37
  • 2
    `Object.assign(this, options);`. – Teemu Mar 15 '22 at 08:37
  • Looks good. Why don't we use colon but equal mark like this ? constructor ({ vehicleType : 'car', name =:'', range =:'', seats : '' } = {}) – Balalayka Mar 15 '22 at 08:54
  • @CelalettinTurgut Because that is how the language architects of JavaScript decided how default values should be set. It's the same as the `options = ...` in your code. – Ivar Mar 15 '22 at 08:57
  • 2
    You can also combine the two first comments, pass the arguments as CherryDT has adviced, then do `Object.assign(this, {vehicleType, name, range, seats});` in the constructor, you'll get rid of all the `this.x = ...` lines. – Teemu Mar 15 '22 at 09:01
  • 1
    @Teemu Did you mine? `constructor(options = { vehicleType: 'car', name: '', range: '', seats: '' }) { Object.assign(this, options) }` – Balalayka Mar 15 '22 at 09:16
  • 1
    Yep, that'll work too. – Teemu Mar 15 '22 at 10:08
  • Thank yo so much. I can accept it as an answer – Balalayka Mar 15 '22 at 10:17

2 Answers2

0

you can do it this way:

class Vehicle {
  constructor(options) {
    const { vehicleType = 'car', name = '', range = '', seats = '' } = options;
    this.vehicleType = vehicleType
    this.name = name
    this.range = range
    this.seats = seats
  }

  getRangeToSeatsRatio() {
    return this.range / this.seats
  }

  multiplySeatsBy(count) {
    this.seats *= count
    return this
  }

  getSeatCount() {
    return this.seats
  }

  get rangeToSeatsRatio() {
    return this.range / this.seats
  }

  
}
walid sahli
  • 406
  • 3
  • 11
-1

You can do it like this:

class Vehicle {
  vehicleType = 'car';
  name = '';
  range = '';
  seats = '';

  constructor(options) {
    Object.assign(this, options);
  }

  getRangeToSeatsRatio() {
    return this.range / this.seats
  }

  multiplySeatsBy(count) {
    this.seats *= count
    return this
  }

  getSeatCount() {
    return this.seats
  }

  get rangeToSeatsRatio() {
    return this.range / this.seats
  }

}