Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, what you have there is right. Some notes:</p> <ul> <li><code>bar</code> is created on every call to the function <code>foo</code>, but: <ul> <li>On modern browsers this is a very fast process. (Some engines may well only compile the <em>code</em> for it once, and then reuse that code with a different context each time; Google's V8 engine [in Chrome and elsewhere] does that in most cases.)</li> <li>And depending on what <code>bar</code> does, some engines may determine that they can "inline" it, eliminating the function call entirely. V8 does this, and I'm sure it's not the only engine that does. Naturally they can only do this if it doesn't change the behavior of the code.</li> </ul></li> <li>The performance impact, if any, of having <code>bar</code> created every time will vary widely between JavaScript engines. If <code>bar</code> is trivial, it will vary from undetectable to fairly small. If you're not calling <code>foo</code> thousands of times in a row (for instance, from a <code>mousemove</code> handler), I wouldn't worry about it. Even if you are, I'd only worry about it if I saw a problem on slower engines. <a href="http://jsperf.com/cost-of-creating-inner-function-v2" rel="nofollow noreferrer">Here's a test case involving DOM operations</a>, which suggests that there is an impact, but a trivial one (probably washed out by the DOM stuff). <a href="http://jsperf.com/cost-of-creating-inner-function/3" rel="nofollow noreferrer">Here's a test case doing pure computation</a> which shows a much higher impact, but frankly even, we're talking a difference of <em>micro</em>seconds because even a 92% increase on something that takes <em>micro</em>seconds to happen is still very, very fast. Until/unless you saw a real-world impact, it's not something to worry about.</li> <li><code>bar</code> will only be accessible from within the function, and it has access to all variables and arguments for that call to the function. This makes this a very handy pattern.</li> <li>Note that because you've used a function <em>declaration</em>, it doesn't matter where you put the declaration (top, bottom, or middle&nbsp;&mdash; as long as it's at the top level of the function, not inside a flow control statement, which is a syntax error), it gets defined before the first line of step-wise code is run.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload