define function within another function in javascript
<pre><code>function foo(a) {
    if (/*some condition*/) {
        // perform task 1
        // perform task 3
    }
    else {
        // perform task 2
        // perform task 3
    }
}
</code></pre>

<p>I have a function whose structure is similar to the above. I want to abstract <em>task 3</em> into a function, <code>bar()</code>, but I wish to limit the access of this function to only within the scope of <code>foo(a)</code>.</p>

<p>To achieve what I want, is it right to change to the following?</p>

<pre><code>function foo(a) {
    function bar() {
        // perform task 3
    }

    if (/*some condition*/) {
        // perform task 1
        bar();
    }
    else {
        // perform task 2
        bar();
    }
}
</code></pre>

<p>If the above is correct, does <code>bar()</code> get redefined every time <code>foo(a)</code> gets called? (worrying about waste of cpu resource here)</p>
