Closed
Bug 944855
Opened 12 years ago
Closed 10 years ago
Firefox 25.0.1 function hoisting: does not work for functions declared inside if statement
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
People
(Reporter: tunghack, Unassigned)
Details
User Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Steps to reproduce:
alert(f);
if (1){
function f(){};
}
Actual results:
ReferenceError: f is not defined
Expected results:
We should see an alert here.
Summary: Firefox 25.0.1 hoisting: does not work for functions declared inside if statement → Firefox 25.0.1 variable hoisting: does not work for functions declared inside if statement
firefox-trunk also has the same problem. I am not sure about other versions.
Summary: Firefox 25.0.1 variable hoisting: does not work for functions declared inside if statement → Firefox 25.0.1 function hoisting: does not work for functions declared inside if statement
Updated•12 years ago
|
status-firefox25:
--- → affected
status-firefox26:
--- → affected
status-firefox27:
--- → affected
status-firefox28:
--- → affected
status-firefox29:
--- → ?
Component: General → JavaScript Engine
Version: 25 Branch → Trunk
Comment 2•12 years ago
|
||
That's not valid ECMAScript 5. Per spec, it should be a syntax error. In practice, browsers support it in various ways; we support it by treating it as syntactic sugar for this valid script:
alert(f);
if (1){
f = function f(){};
}
which should in fact lead to f being undefined at the alert site.
There are proposals for ECMAScript 6 or 7 to allow a function declaration in a block, but then it would be block-local, which would still make your case not see a defined f. See bug 585536. Though it's not clear this will ever be standardized; see https://bugs.webkit.org/show_bug.cgi?id=27226 and especially https://bugs.webkit.org/show_bug.cgi?id=27226#c4
Note that in strict mode the script does in fact produce a SyntaxError.
In any case, as far as I can tell this is either invalid or a duplicate of bug 585536.
status-firefox29:
? → ---
I agree. This is not a bug. In ECMAScript 5, an 'if statement' is specified as follows:
IfStatement :
if ( Expression ) Statement else Statement
if ( Expression ) Statement
A function declaration is not a statement (http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf#page=98), hence it should be a syntax error. However, many browsers support function declaration as a statement, which is non-standard. Chrome supports this by, I guess, converting function declaration into a variable assignment statement: function f(){} --> var f = function f(){}. As a result, f is hoisted.
I think what Chrome does is consistent with what we understand about function hoisting. What I do not understand is why Firefox decides to do it differently.
I meant Chrome supports this by converting function declaration into variable declaration and assignment.
Comment 5•12 years ago
|
||
> What I do not understand is why Firefox decides to do it differently.
For one thing, because Chrome's behavior is pretty bonkers when you do this:
if (1)
function f() { alert('pass'); }
else
function f() { alert('fail'); }
f();
in which case it alerts 'fail'.
So Chrome does not really convert those function declarations into variable declarations and assignments, otherwise they should not have this weird behavior. They probably just move those function declarations out of 'if' and 'else', or something similar. Nevertheless, I still do not understand why Firefox does not convert function declaration into variable declaration and assignment in this case.
Comment 7•12 years ago
|
||
We convert it into assignment, but without a var declaration, apparently. And the assignment is placed at the function expression point.
Could you describe the exact behavior you were expecting here and why? And don't forget to address the issues regarding with and catch talked about in the webkit bug!
When I see a function declaration, I expect it to be hoisted. I think this expectation is reasonable and consistent with what we understand about function hoisting in JavaScript.
Anyways, are you sure that Firefox always converts function declaration into assignment inside 'if'? How about 'with' statement? How does Firefox handle this code?
a = {b:1}
with(a){if(1){function b(){}}}
Comment 9•12 years ago
|
||
> are you sure that Firefox always converts function declaration into assignment inside
> 'if'?
Nope. ;) Just what I have from black-box testing. I haven't dug into the actual implementation.
Comment 8 doesn't answer the concrete question from comment 7, for what it's worth. But also, if you read the webkit bug you'll realize that the standards group has spent a while trying to figure out what to do here and is stymied because of compatibility constraints: web pages depend on the behavior differences you're observing. :(
Comment 10•12 years ago
|
||
Of course in terms of your own code, the right thing to do is to use strict mode.
| Reporter | ||
Comment 11•12 years ago
|
||
I agree it is not easy to tell what to do here. However, I don't think I like the way Firefox handles this. I would choose to deal with this by converting function declaration into variable declaration and assignment for the following reasons:
1. They are equivalent: function f(){} is semantically equivalent to var f = function f(){}.
There are two differences between function f(){} and f = function f(){}
a) If they are placed in the global scope, f in the former is not configurable and f in the latter is configurable.
b) If they are placed in the function scope, the latter might make a change to the outer scope while the former remains local.
2. You do not have to deal with 'with' like the example in my comment 8.
3. Even though the function is not hoisted, at least its name is hoisted.
| Reporter | ||
Comment 12•12 years ago
|
||
My mistake :). You still have to deal with 'with' in this case.
Comment 13•10 years ago
|
||
No longer reproducible, resolving as works-for-me.
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago
Resolution: --- → WORKSFORME
You need to log in
before you can comment on or make changes to this bug.
Description
•