CoffeeScript provides us with a very nice syntax for creating classes.
Here is the equivalent code in JavaScript:
We can use the same syntax for creating internal / private classes. (I don’t know if there’s a correct name for it, but I’m basically referring to a class which is defined within the parent class.)
This works as you would except - we can access the internal / private class from within the parent class (Dog), but not from outside it.
We can also expose this internal class to outside callers - we can effectively treat the parent class as a namespace.
I find this especially useful for testing. For example, you may find it necessary to extract some piece of functionality into an internal class, but if we didn’t expose that internal class in this way there would be no way to test it. It might seem messy to expose an internal member purely for testing, but I think this is a safe middle ground - it is still very obvious that the internal / private class should only be used in the context of the parent class.