Creating namespaces

In order to reduce the negative impact of using the global scope, we should avoid creating global objects unless they are absolutely necessary. A first approach to avoid the global scope pollution and organizing our code is to use namespaces. A namespace is a collection of names used to identify objects in a given context. The purpose of namespaces is to avoid confusion and collisions with objects having the same names in another context, providing a way to group names together by category.

Many programming languages support namespaces in order to organize blocks of functionality in our application into easily manageable groups that can be uniquely identified. JavaScript has no built-in support for namespaces, but we can use ...

Get Mastering JavaScript Object-Oriented Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.