Caching the return results of a function from John Resig's Learning Advanced JavaScript
<p>I have a few questions about this function from John Resig's exercise #19 in <a href="http://ejohn.org/apps/learn/#19" rel="nofollow">http://ejohn.org/apps/learn/#19</a></p>

<ol>
<li><p>what is the purpose of the second last line <code>getElements.cache = {};</code> Is it storing <code>return results</code> in an array?  </p></li>
<li><p>If my guess in (1) is correct, is it only catching <code>return results</code> because, in the "else" section of the function, <code>getElements.cache[name] = results;</code>?</p></li>
<li><p>when I played with the code in the console in the tutorial, I removed the line <code>getElements.cache[name] = results</code> from the "else" section, but still got the same result, as when it was there--namely, it told me that there were 76 elements. What, therefore, is the purpose of this line <code>getElements.cache[name] = results</code> if it`s not necessary to get the result?</p></li>
<li><p>is there any significance to the fact that, in the "else" section of the function, the order of the line <code>getElements.cache[name] = results;</code> is reversed from the <code>if</code> section of the code, where it says <code>results = getElements.cache[name]</code></p></li>
<li><p>finally, is <code>cache</code> a pre-defined function in JavaScript? I can't find it in the documentation. </p></li>
</ol>



<pre><code>function getElements( name ) { 
  var results; 

  if ( getElements.cache[name] ) { 
    results = getElements.cache[name]; 
  } else { 
    results = document.getElementsByTagName(name); 
    getElements.cache[name] = results; 
  } 

  return results; 
} 
getElements.cache = {}; 

log( "Elements found: ", getElements("pre").length );
</code></pre>
