Where should private methods be placed when using the JavaScript Module pattern?
<p>I've started using the Module pattern in a project of mine. As I understand it, it goes something like this:</p>

<pre><code>var obj = (function(foo){
     //Some initialization logic up here.

     //Private methods
     var privateBazz = function(){
         return "I'm known only to this closure!";
     }();

     //Public methods
     return { 
         publicFoo: foo,
         publicBar: function(){
             return foo + privateBazz;
         }
     }
})();
</code></pre>

<p>This looked pretty good on paper and, in practice, seems to work reasonably well. Initialization logic at the top is intuitive, then private methods, then public. </p>

<p>I've encountered one issue, though. How should I call publicFoo or publicBar from outside the scope of the return statement and inside the scope of obj's function declaration? </p>

<p>My current solution is to do something like:</p>

<pre><code>var obj = (function(foo){
     //Private methods declared early for use.
     var privateBazz = function(){
         return "I'm known only to this closure!";
     }();

     var privateBar = function(){
         return foo + privateBazz;
     };

     //Some initialization logic up here.
     var dependentOnBar = privateBar();

     //Public methods
     return { 
         publicFoo: foo,
         publicBar: privateBar
     }
})();
</code></pre>

<p>This works, but suddenly the declarations of my private variables are placed above my objects private property declarations. The problem becomes exacerbated if I attempt to keep the private function declarations as close to the code which first calls them, so I have just been declaring all the private functions I need at the top, then initializing properties afterwards. Again, this works, but I am used to having code as close to the execution point as possible. So, declaring blocks of private functions at the top is really awkward to me. Does anyone else feel this way, or is this something I just need to get over for JavaScript? Are there any steps I should be taking when I see this happening?</p>
