Main menu:

Site search

Categories

Archive

JavaScript Namespaces

I do a lot of development in C# and have grown accustomed to namespaces for organizing code. The other day I was working in JavaScript and it hit me – I want to break up some similar functions into different name spaces. For example, if you were writing one large JavaScript file with two functions that cleared out two custom forms. You cannot name both functions clearFields() because they clear different inputs. Instead, you would have to make two functions like clearLoginFields() and clearSignUpFields(). That’s not convenient.


JavaScript does not technically have namespaces, however we’re in luck because we can create pseudo-namespaces! Using a hash table, we can implement the functionality of a JavaScript namespace.