title: JavaScript architecture - mediators, when to use them?
body: <p>This is more of a general question about the structure of my JavaScript code and if I'm going in the right direction towards well structured code.</p>
  
  <p>The current code I've got:</p>
  
  <pre><code>(function (myNamespace, $, undefined) {
      myNamespace.className = {
         init:function { } // do stuff
      }
  } (window.myNamespace= window.myNamespace|| {}, jQuery)));
  
  (function (myNamespace, $, undefined) {
          myNamespace.className2 = {
             init:function { } // do stuff
          }
  } (window.myNamespace= window.myNamespace|| {}, jQuery)));
  </code></pre>
  
  <p>Obviously with the above code, I can use the same Namespace (as per page/site section) and call them via <code>myNamespace.className.init()</code> etc. I can also combine these if I want to, but I'm encapsulating classes for readability.</p>
  
  <p>Now, I've been reading <a href="http://addyosmani.com/largescalejavascript/" rel="nofollow">http://addyosmani.com/largescalejavascript/</a> about the concept of mediators. My secondary question is when (and if) I should be using these? From className2 obviously I can do:</p>
  
  <pre><code> myNamespace.className2 = {
                 init:function { myNamespace.className.init() } // do stuff
              }
  </code></pre>
  
  <p>So why would this ever subscribe to className like <code>mediator.subscribe("classNameInit")</code> and publish that event in className? </p>
  
  <p>I'm highly open to suggestions about the structure of my code as this is something I need to get right whilst I'm changing the way I write my JavaScript.</p>