I want to make unique instances of a set of cars that different people can hold. The cars will have similar base specs but some of their properties and methods will vary.
The problem I have is that I can't work out how this should work. How do you deal with or create instances of instances in JavaScript?
var Car = function(make, country) {
this.make = make;
this.country = country;
};
var Ferrari = new Car('Ferrari', 'Italy');
var fred = new Person() {};
var fred.cars['Ferrari'] = new Ferrari(1200, 300000);
This causes this error, for obvious reasons. I am aware it is not a constructor (see below).
Uncaught TypeError: Ferrari is not a constructor
What I am looking for is something like this. Each different instance of a Ferrari will have a different price and milage.
var Ferrari = function(currentPrice, miles) }
this.currentPrice = currentPrice;
this.miles = miles;
// this is an instance of car, aka it needs the result of this:
// new Car('Ferrari', 'Italy');
};
Fred's Ferrari is an instance of Ferrari, which is an instance of Car. The problem is that I can't think of a way to make a constructor build a constructor. Is there a way to do this, or am I just going about this in the wrong way?
Other Notes:
I know I could essentially just make each type of car a static JSON-like object and then make instances of that and add new unique values. However, I would like to be able to keep the Car as a constructor so I can easily make more when I need to.
I am clearly missing some understanding of OOP or JavaScript here, but it would be great if someone could point me in the right direction.