Namespace can dramatically reduce the number of globals required and at the same time prevents the collisions or excessive name prefixing .
Refactor anti-pattern to Namespace example
Consider this example
in this example all the functions,variables and objects are declared and polluting the global scope of your application . You can refactor this type of code by creating a single global object for your application , called for example Student and change all functions and variables to become properties of your global object
This pattern is good way to namespace your code and avoid naming collisions not only in your own code but collisions between your code and third-party code on the same page .
Drawbacks of Namespace
- More to type , prefixing every variable and function adds up in the total amount of code that needs to be downloaded
- Only one global instance as a result any part of the code can modify the global instance and the rest of the functionality gets the updated state
- Long nested names = slower property resolution lookups
Top comments (1)
Or use modules