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

102
Visualizações
Unable to call method on object created from class

I have built a class and created an object from it. However, when viewing in the console I am unable to call the method changeColor on the object as it is 'not defined' but I appear to be looking right at it... What am I missing please?

This is my class constructor script called car.js:

class NewCar {
    constructor (
        make,
        model,
        year,
        seats,
        color,
        price,
        hybrid,
    )
    {
        this.make = make;
        this.model = model;
        this.year = year;
        this.seats = seats;
        this.color = color;
        this.price = price;
        this.hybrid = hybrid;
    };

    changeColor (newColor) {
        this.color = newColor;
    }
}


export default NewCar;

Here is the script that imports and uses the constructor to build a volvoxc40 object from it, called script.js.

import NewCar from "./car.js"

const volvoxc40 = new NewCar(
    "Volvo",
    "xc 40",
    2022,
    5,
    "red",
    20000,
    true,
);

console.log ('The new car: ', volvoxc40
);

And for completeness, here is the HTML which references the 2 scripts:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Classes</title>
    <script type="module" src="car.js"></script>
    <script type="module" src="script.js"></script>
  </head>
</html>

Thank you

about 4 years ago · Juan Pablo Isaza
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