I have a ES6 Vehicle class defined below. I want to have a static method such that I can use with or without parenthesis. I have defined static AvailableTypes and static AvailableTypes() but it gives an error TypeError: Vehicle.AvailableTypes is not a function
class Vehicle {
constructor({ vehicleType = 'car', name = '', range = '', seats = '' }) {
this.vehicleType = vehicleType
this.name = name
this.range = range
this.seats = seats
}
getRangeToSeatsRatio() {
return this.range / this.seats
}
get rangeToSeatsRatio() {
return this.range / this.seats
}
static AvailableTypes = ['car', 'plane']
static AvailableTypes() {
return this.AvailableTypes
}
}
const vehicle = new Vehicle({ name: 'My Car name', seats: 500, wheels: 45 })
console.log(Vehicle.AvailableTypes) // (2) ['car', 'plane']
console.log(Vehicle.AvailableTypes()) //TypeError: Vehicle.AvailableTypes is not a function
This should result in availableVehicleTypes being ['car', 'plane']
const availableVehicleTypes = Vehicle.AvailableTypes;
This should also result in availableVehicleTypes being ['car', 'plane']
const availableVehicleTypes = Vehicle.AvailableTypes();
It is impossible to create a value that is both a function and an array.
Make up your mind and choose only one of the two, it'll be much less confusing to the users of your class.