Function context using callback
<p>Why these two different code produce different results?</p>

<p>I mean <code>foo2</code>, defined in the first code, and <code>callback</code>, in the second code, are the same and they are executed in the same context, inside <code>foo</code>.</p>

<p>First code:</p>

<pre><code>var foo = function () {
   var bar = 2;
   var foo2 = function () {
     console.log("bar: ", bar);
   }
   foo2();
}

foo() // 2
</code></pre>

<p>Second code:</p>

<pre><code>var foo = function (callback) {
   var bar = 2;
   callback();
}

var callback = function () {
   console.log(bar);
}

foo(callback); // ReferenceError: bar is not defined
</code></pre>
