title: How to prevent an infinite recursion
body: <p>I am trying to recursively print out the contents of <strong>jQuery</strong>. I'm planning on using this to analyze an existing instance of jQuery (loaded in a page) against a small database of known "jQuery signatures", to determine what changes have been made (i.e. what plugins have been loaded, functions modified, etc.). </p>
  
  <p>To do this, I have this small function:</p>
  
  <pre><code>function recurse(obj, iter){
      var padding = (new Array(iter + 1)).join("&amp;nbsp;&amp;nbsp;") + "&gt;";
  
      for (var i in obj){
      document.writeln(padding + i + "&lt;br/&gt;");
  
      if (iter &lt; 5)
          recurse(obj[i], iter + 1);
      }
  }
  </code></pre>
  
  <p>When I execute this:</p>
  
  <pre><code>recurse(jQuery, 1);
  </code></pre>
  
  <p>I get something like this:</p>
  
  <pre><code>  &gt;prototype
      &gt;init
        &gt;prototype
          &gt;init
            &gt;prototype
          &gt;selector
          &gt;jquery
            &gt;0
  
  .... On and on and on .....
  </code></pre>
  
  <p>My problem is, at the very beginning, you can see that <code>prototype</code> and then <code>init</code> repeat over and over again. The only reason it stopped at 5 deep is because of the <code>if (iter &lt; 5)</code> check. If the limit wasn't there, it would have recurred [sic?] forever. The iteration limit helps, but what if there is a critical function 6 deep? Essentially, I have no idea what I should make this iteration limit, or if there should be one at all.</p>
  
  <p>Instead, I'm thinking there must be some kind of algorithm that can prevent never-ending recursion. Does such an algorithm exist? Or should I change how I go about traversing jQuery? Thanks,</p>