title: Is it recommended to put a function inside .each()?
body: <p>I am looping through a collection of DOM elements. Each iteration makes use of a <code>helper()</code> function several times like so:</p>
  
  <pre><code>$(".myclass").each(function() {
      var arg;
      helper();
      // do stuff
      function helper() {
          // do helpful stuff with arg
      }
  });
  </code></pre>
  
  <p>Is it bad practice to include the <code>helper()</code> function inside the <code>function()</code> block as above? Should I use:</p>
  
  <pre><code>$(".myclass").each(function() {
      var arg;
      helper(arg);
      // do stuff
  });
  function helper(arg) {
      // do helpful stuff with arg
  }
  </code></pre>
  
  <p>In the first example will every matched <code>.myclass</code> element have its own instance of the <code>helper</code> function in memory?</p>
  
  <p><strong>Edit</strong>: If anyone is looking for a good explanation of the answer - <a href="http://net.tutsplus.com/tutorials/javascript-ajax/stop-nesting-functions-but-not-all-of-them/" rel="nofollow">http://net.tutsplus.com/tutorials/javascript-ajax/stop-nesting-functions-but-not-all-of-them/</a></p>