Is there a way of adding an object without having to declare a const?
function Car (make, color) {
this.make = make,
this.color = color,
this.greet = function () {
console.log(`The make is ${this.make} and color is ${this.color}`);
};
}
const newObject = "Toyota"
const ford = new Car("Ford", "blue")
ford.greet();``
const vauxhall = new Car("Vauxhall", "green")
vauxhall.greet();
I thought I may be able to have:
const newObject = "Toyota"
Then add Toyota in place of ford or vauxhall.
2
Answers
There are 4 ways of creating objects:
There is NO 5th way – and I don’t think you can on the fly create any object you want. The best you close in is — Object Literal.
You can create a Car class and define the setter method of "make" and "color".