Javascript namespacing

Namespaces help you organise code so that it’s easy for others to find their way around it. In Javascript, they also minimise the number of global variables.

Javascript doesn’t (at the time of writing) have a dedicated way to namespace components. But we can do it using object literals.

I’ll show you how to namespace your components with an example application. The application represents a zoo. The zoo has a couple of animals plus some additional information.

This is the directory structure for the zoo:

zoo/ zoo.js zoo.information.js animals/ zoo.animals.js zoo.animals.Penguin.js zoo.animals.Tiger.js

The root namespace resides inside zoo.js.

See original post