Namespacing - mmedrano9438/peripheral-brain GitHub Wiki

Namespacing is the practice of creating an object which encapsulates functions and variables that have the same name as those declared in the global scope. A namespace can be declared in multiple ways and the most common way of declaring a namespace involves creating an object.

creating a namespace in javascript is as follows:

keyword scope_name={}

The keyword may be var, let, or const. Please note that, when the const variable is used, the namespace cannot be altered or modified after declaration.

  • Namespace prevents the pollution of the code base and makes the code cleaner and easy to understand.
  • prevents unexpected collision of variables or functions.
  • Overriding a function or variable is possible if everything is declared in the global namespace. This will not result in a [compiler] error but gives logical errors.
  • Namespace becomes very essential while dealing with third-party libraries, as we may override the functions in those libraries.