3
console.log(a());
function a(){
    console.log("hello");
}

From above code, i will expect "hello" (and some undefineds) to be logged on console. But firebug gives

ReferenceError: a is not defined

So firebug does not do hoisting?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Rizwan Sharif
  • 1,089
  • 2
  • 10
  • 20
  • Are these run from a script or from Firebug's console? – Mitya Jul 27 '12 at 18:25
  • from firebug console ... @Josh , i will expect javascript hoisting to kick in and a() should become visible . If you will run it from html file , it will work. only firebug is not doing hoisting – Rizwan Sharif Jul 27 '12 at 18:31
  • Just to be totally clear: This happens when you paste the code into Firebug’s console. – Josh Lee Jul 27 '12 at 18:40
  • http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ - Another blogger confirms that Firebug doesn't do hoisting in certain scenarios. Search for 'Firebug' on that page. – Amith George Jul 27 '12 at 18:48

1 Answers1

7

The reason for the issue is that

functions do not hoist when declared inside a child block.

by MDN (Much covered here is not standard ECMAScript).

Compare the following snippets:

alert(c());
function c(){return 42;}

and

{
    alert(c());
    function c(){return 42;}
}

The first one will alert 42, whereas the second one will throw ReferenceError.

And here is the code that gets executed when you are playing with Firebug: Firebug's tooltip

data;
with(_FirebugCommandLine){ // >> block begins
    console.log(a());
    function a(){
        console.log("hello");
    }
} // << block ends

Update
The behavior observed seems to be a glitch in Firefox javascript engine because it is not observed in chrome and IE9, see this fiddle.

Li0liQ
  • 11,158
  • 35
  • 52
  • +1 for finding the reason. Though its a pity that atm MDN wiki doesn't show the code samples. Its mostly filled with `reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown.` errors... – Amith George Jul 27 '12 at 18:53
  • Could you explain what's going on in this fiddle http://jsfiddle.net/t3g4b/ ... If the function didn't hoist, why is the output 8? – Amith George Jul 27 '12 at 18:58
  • I think you meant to link to: https://developer-new.mozilla.org/en-US/docs/JavaScript/Reference/Scope_Cheatsheet#function_oddities .. Notice the `-new` before `mozilla.org` in the url... The url without the `-new` doesn't have all the code snippets. – Amith George Jul 27 '12 at 19:08
  • Sure, as already mentioned, it is blocks that trigger that weird behavior. Check http://jsfiddle.net/FZ92S/ . Moreover, it looks like Chrome and Firefox interpret them differently hence 8 for Chrome and 3 for Firefox. – Li0liQ Jul 27 '12 at 19:16
  • http://jsfiddle.net/t3g4b/2/ ... This behaviour seems to be a mozilla specific thing. The linked fiddle works as expected in IE 9 and Chrome latest stable. In FF latest stable, it gives the error. – Amith George Jul 27 '12 at 19:17
  • Yeah, I just realized that. Nice, I learnt something new today :) ... I went to FF only to test the OP's code in Firebug, the rest I was doing from Chrome. – Amith George Jul 27 '12 at 19:20
  • The same for me, actually :-D. It took quite a time to understand the reason. – Li0liQ Jul 27 '12 at 19:21
  • 1
    It seems that this is not a glitch -- function statements are simply [not valid in blocks at all](http://stackoverflow.com/questions/8871974/why-are-function-declarations-handled-differently-in-different-browsers/8871984#8871984) (also https://whereswalden.com/2011/01/24/new-es5-strict-mode-requirement-function-statements-not-at-top-level-of-a-program-or-function-are-prohibited/). The fact that the interpreter doesn't die immediately upon finding a function statement in a non-top block is because all browsers extend the ECMAScript spec, but do it in different ways. (cc: @AmithGeorge) – apsillers Jun 05 '13 at 16:14