Reputation: 331
I'm used to being able to in Java define an object which can contain other objects as members, for example (psuedocode):
class Zoo{
private List<Animal> animals;
}
class Animal {
private double weight;
private double height;
private double species;
}
Then you could have a constructor for zoo which takes X animals and adds them to an animal collection and have it's own methods.
In coffeescript I can't seem to be able to do this, is this a limitation of javascript?
Upvotes: 2
Views: 44
Reputation: 4533
hope I understood your question.
in Coffeescript you may write
class Animal
name: ''
class Zoo
animals: [] #notice you do not specify type!
constructor: (animalList) ->
@animals = animalList #and animal list is an array of Animal class instances
zoo = new Zoo([new Animal()])
console.log(zoo.animals.length) #should be eq to 1
If you want animals to be private same as it would be in Java or C#, I would recommend not using classes but:
Zoo = (animals) ->
return {
getAnimals: -> animals
addToAnimals: (animal) -> animals.push(animal)
}
Upvotes: 4