Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
How can I define a static method in JavaScript to use with or without parenthesis?

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();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!