Structuring clean Javascript

Status
You're currently viewing only EmeraldArcana's posts. Click here to go back to viewing the entire thread.
Not open for further replies.
Hey all. I'm doing some Javascript work (it's an educational game). I've been doing most of my stuff using the Module pattern, which looks something like the following:

Code:
var MyModule = (function() {
  var exports;
  exports.dance = function(type) {
    return type;
  }
  return exports;
})();

But I occasionally see in this codebase I'm working on some other pattern that I don't know the name of:

Code:
var MyOtherModule = {
  process: function(text) {
    return "";
  },
  startup: function() {
    return "Start up";
  },
  shutdown: function() {
    return "Shut down";
  }
}

One thing I figured out was that the second method doesn't allow you to use methods in the other methods (ex: I can't call shutdown within process) without some weird magic that I haven't figured out yet. What's the purpose of the second type of structuring? I haven't quite figured it out yet. Is it for C-style "static" methods in namespaces?

I've been occasionally browsing the numerous blog posts on the Internet about Javascript but I still haven't really wrapped my head around the principles of the language and how to "architect" good Javascript yet. Wondering if anyone here can help provide some specific guidance - and possibly even an example of a small, but good open source project that I can look at and be like, "Wow that's some nice Javascript source code".

Thanks for your assistance.
 
Status
You're currently viewing only EmeraldArcana's posts. Click here to go back to viewing the entire thread.
Not open for further replies.