Reputation: 149
I have two constructors in my project. The first is information about a gym and the second is all the personal trainers at that gym.
It looks something like this
class Gym {
constructor (
id,
name,
GymId,
address,
imageUrl,
description
)
}
class Trainer {
constructor (
id,
name,
gymID,
yearsExperience,
Gender,
description,
imageUrl
)
}
I link the trainers with the gym by checking if the gymId's are equal. my question is, would it be better to nest the trainer data in the gym class. so my gym class would have an extra property like trainers
. My data may look something like this:
new Gym (
0,
'Fitness First',
0,
'some random address',
'image link'
'This is a cool gym'
[{id: 0, name: Jim, 0, 10, Male, 'Good Guy', 'imageURL'}, {...}, {...}]
)
Upvotes: 1
Views: 112
Reputation: 3281
I think you are misunderstanding what a constructor is. To directly answer your question, no you shouldn't separate them. Why? Because they are inherently two different objects.
Since they are two different objects, they need their own constructors. Because the job of a constructor is to simply initialize the object, nothing else.
Jmrk is right in his solution. You can simply create the trainer class, and create a list of trainers, then pass the list of trainers to the Gym object. In the Gym object, make sure that the constructor takes in a list of trainers.
Upvotes: 0
Reputation: 40511
I think there are two separate questions here:
(1) Should I use a class with a constructor, or just an object literal {id: 0, name: "Jim", gymID: 0, ...}
?
I think that one has a fairly clear answer: when you have several objects of the same kind/shape, then you should use a class, and that class should have a constructor. (Arguably this is somewhat opinionated, but I would assume it's reasonably non-contentious.)
(2) In the specific case of Gym
and Trainer
, should the Gym
class have a property with its list of Trainer
s?
That's a design decision that really depends on your app's needs; I don't think it's possible to give a general answer to this. If you often have the situation that you have a gym
instance and want to iterate over its trainers, then having that list pre-populated is the more efficient way than filtering through a huge global list of all trainers of all gyms. If you often reassign trainers to a different gym, then only needing to update that one property is easier and faster than finding the two gyms and removing the trainer instance from one list and adding it to the other.
If you decide for a list of trainers, you can still use the Gym
constructor to populate it. The Trainer
class then may or may not need a gymID
field, depending on whether you need it. Example code with both features:
class Gym {
constructor(id, name, ..., trainers) {
this.id = id;
// ...
this.trainers = trainers;
for (let trainer of trainers) {
trainer.gymId = this.id;
}
}
}
let trainers = [
new Trainer(0, "Jim", ...),
new Trainer(1, "Mary", ...)];
let gym = new Gym(0, "Fitness First", ..., trainers);
Upvotes: 1