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