Making the most of Javascript namespacing

Posted on 29 October 2008 (05:33 PM)

Today's article by Robert Nyman about Javascript namespacing remembered me about my own approach, which I've been using for quite a few projects now.

If you have no clue about Javascript namespacing, go and read Robert's article. I'll wait.

Back? Good :)

The Javascript single namespace approach is, in my opinion, a great thing. I have used it quite a lot and have even set up a code snippet in Textmate which I use as a spring board for every single script I've written over the past months.
After reading Robert's article, I realized more people might benefit from the way I approach this design pattern. Here goes...

Automatically initializing your namespace

My script files always look a little something like this:

  1. // when developing for the XYZ Corporation:
  2. var XYZCorp = {
  3. // modules go here
  4. };
  5. XYZCorp.init ();
  6. Download this code: /code/making_the_most_of_javascript_namespacing1.txt

Every module used on the website resides inside the global XYZCorp object. After defining the XYZCorp object, I execute its init function. This init function looks a little something like this:

  1. // when developing for the XYZ Corporation:
  2. var XYZCorp = {
  3. init: function () {
  4. for (var i in this) {
  5. if (typeof this[i].init == 'function') {
  6. this[i].init ();
  7. }
  8. }
  9. }
  10. };
  11. XYZCorp.init ();
  12. Download this code: /code/making_the_most_of_javascript_namespacing2.txt

That's it! I loop through all members of the global XYZCorp object, and if a member contains an init method, I execute it, successfully firing up the module.

This is a very easy approach, which can be reused over and over and over again, and if you ever wish to append a new module to the site, just stuff it inside the global namespace and give it an init method. Here's an example:

  1. // ...
  2. myModule: {
  3. init: function () {
  4. this.say ('myModule initiated!');
  5. },
  6. say: function (msg) {
  7. alert (msg);
  8. }
  9. },
  10. // ...
  11. Download this code: /code/making_the_most_of_javascript_namespacing3.txt

If you would like to use my template in Textmate, just copy the following code into a new snippet inside the Bundle Editor:

  1. /**
  2. * ---------------------------
  3. * Client: ${1:client-name}
  4. * URL: ${2:client-url}
  5. * Author: ${3:author-name}
  6. *
  7. * ---------------------------
  8. */
  9. /**
  10. * General namespace
  11. */
  12. var ${4:global-namespace} = {
  13. /**
  14. * Initialize all objects
  15. */
  16. init: function () {
  17. for (var i in this) {
  18. if (typeof this[i].init == 'function') {
  19. this[i].init ();
  20. }
  21. }
  22. },
  23. $5
  24. };
  25. /**
  26. * Initialize general namespace
  27. */
  28. $4.init ();
  29. Download this code: /code/making_the_most_of_javascript_namespacing4.txt

Then assign it to a Tab Trigger and make sure you point the Scope Selector to source.js.

Back to top

Filed under Javascript

Comments:

  1. 29 October 2008 (05:44 PM) by Robert Nyman

    Interesting! When we developed the plug-in architecture for DOMAssistant, we also added support for automatically running any init methods it could find - quite handy!

    I just might take you up on that for my regular JavaScript coding as well! However, I need to find a good way, for most project init code for me has to run when the DOM has loaded, so I guess I need to add finding that out as well (most likely through the JavaScript library of choice).

    this comment has been quoted by Harmen Janssen

  2. 30 October 2008 (09:42 AM) by Harmen Janssen

    Robert Nyman wrote:

    Interesting! When we developed the plug-in architecture for DOMAssistant, we also added support for automatically running any init methods it could find - quite handy!

    I just might take you up on that for my regular JavaScript coding as well! However, I need to find a good way, for most project init code for me has to run when the DOM has loaded, so I guess I need to add finding that out as well (most likely through the JavaScript library of choice).

    Do take me up on that, Robert :)

    About the DOMready event; I've adopted the best practice to include my Javascripts at the bottom of the HTML file. That way, you can begin executing your script immediately.

    On the other hand, you might want to ensure your scripts are portable by wrapping 'em up in a DOMready event... Food for thought!

Leave a comment

RE: Making the most of Javascript namespacing

Note to spammers: rel="nofollow" will be added to links. If I consider your comment spam, your IP-address might get blocked.

HTML not allowed. URLS will be auto-linked. Maximum length is 1250 characters.

I understand this is inconvenient, but the spam I'm receiving on this website is driving me nuts. Please forgive me and cope with it until I find a better solution.

Mandatory fields are marked by an asterisk (*).

Increase textarea-size Decrease textarea-size

Back to top

Preferences

These settings will be saved for your convenience.