Closed
Bug 693718
Opened 14 years ago
Closed 14 years ago
JS Error When Referencing Later Defined Function
Categories
(Firefox :: General, defect)
Tracking
()
RESOLVED
DUPLICATE
of bug 593176
People
(Reporter: zgardner, Unassigned)
Details
Attachments
(1 file, 1 obsolete file)
|
509 bytes,
text/plain
|
Details |
User Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1
Steps to reproduce:
I ran the following JS:
function outerFunction(inOptions) {
inOptions.functionToCall.apply(this, []);
}
outerFunction({
functionToCall:myFunction
});
function myFunction() {
alert("Zach");
}
Actual results:
I got a JS error that myFunction is not defined.
Expected results:
The same thing that happens when I wrap it in a closure:
(function() {
function outerFunction(inOptions) {
inOptions.functionToCall.apply(this, []);
}
outerFunction({
functionToCall:myFunction
});
function myFunction() {
alert("Zach");
}
})();
This works correctly. As does a simplified version:
(function() {
function outerFunction(inFunctionToCall) {
inFunctionToCall.apply(this, []);
}
outerFunction(myFunction);
function myFunction() {
alert("Zach");
}
})();
But when I remove the simplified version from its closure, it doesn't work either:
function outerFunction(inFunctionToCall) {
inFunctionToCall.apply(this, []);
}
outerFunction(myFunction);
function myFunction() {
alert("Zach");
}
Could you wrap these scripts into a simple page testcase and attach them here using the "add an attachment" link? So that they can be run directly inside Firefox.
Attached an example as requested. It seems my original diagnosis of the problem was incorrect. It doesn't always happen at the global scope, just when it's put within an if/else or a try/catch.
And what is the code expected to do? I got 4x alert("Zach"). That seems correct to me. What do you get?
Look at the source. There are six times that the alert function should have been called. You only saw it happen 4 times, so something must be wrong.
If you open firebug, you'll see that there are two JS errors. Each correspond to the call that happened in the "This Doesn't Work" sections.
When wrapped in an if/else or a try/catch, functions aren't able to be accessed until the JS engine hits that line. This behavior does not happen with either Chrome or IE, so I'm assuming it's a bug with the interpreter.
Ah, sorry, somehow I saw only 4 blocks.
Ok, there are 6 functions and they alert 6 times in IE8.
However, what is the difference between function A and D, B and E , D and F? Where is the try/catch? Functions B and E do not work in Firefox. They both seem to be wrapped in "if (true)".
This file shows a simplified version of the problem.
The expected behavior when defining a function is that you will have access to it at any point within it's defined scope, even if you're executing code before it hits the function definition. The first two "This Works" blocks show this happening at the global scope. The calling of aFunction and bFunction both work because before either are called the interpreter sees that both should be defined and available.
There is a problem in the interpreter when the definition of the function is wrapped in an if/else or a try/catch. Once the if branch is taken, other browsers consider that any function inside that branch should be available before the line it's on is specifically executed. Firefox on the other hand only defines that function once the line it's on is executed, meaning references to it before the definition line does not work.
Attachment #566505 -
Attachment is obsolete: true
OK, this seems to be Bug 593176. This non-functionality seems intentional (according to spec).
See also bug 561670, bug 434912. They point to bug 585536 which may be of interest to you.
Cool, thanks for the insight. I wasn't sure what to search for, but those do definitely seem like related bugs. If the specs for ECMAScript state that functions defined inside blocks are block-scoped and are by definition different than globally defined functions, then I'm not going to disagree with that.
While writing test cases for this bug, I found a possible bug in Chrome and IE. (http://code.google.com/p/chromium/issues/detail?id=100037)
if(true) {
function myFunction() {
alert("A");
}
}
else {
function myFunction() {
alert("B");
}
}
myFunction(); // Will always alert B in Chrome and IE, A in FF
Because Firefox delays defining myFunction until one of the definitions is explicitly executed, it correctly defines the function. Chrome and IE use the definition in the else branch, which is never executed.
Status: UNCONFIRMED → RESOLVED
Closed: 14 years ago
Resolution: --- → INVALID
You need to log in
before you can comment on or make changes to this bug.
Description
•