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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.