Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

101
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda