Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda