I found myself coding this pattern, which seems like an abuse of javascript but also seems to work:
zoo.animals = [cat, dog, lemur]
zoo.animals.currentlySelected = zoo.animals[0]
So I'm creating an animals
array, but then realizing I also want a named property of animals
called currentlySelected
. I guess actually I end up with an array called myapp.things and a separate object with the same name? I like that the list of animals and the knowledge of which one is currently selected are both in the zoo.animals namespace. Am I likely to be causing unforseen problems here?
n.b. : I expect people will say that the correct way to do this is
zoo.animals.listOfAll = [cat, dog, lemur]
zoo.animals.currentlySelected = zoo.animals.listOfAll[0]
but my question is more about how and why my way is wrong rather than what alternate approach is the best.