Standard reference function declarations and function expressions
There are two ways of defining a function: function declaration and function expression.
-
function declaration:
-
function Identifier (formalparameterlist
Opt
) {Functionbody}
-
function expression:
-
function Identifier
Opt
(FormalParameterList
Opt
) {Functionbody}
ECMAScript the function declaration and function expression according to the context, suppose "function test () {}" is part of an expression, it is a function expression, otherwise it is a function declaration.
For more information about function declarations and function expressions, refer to the contents of the ECMAScript specification in the Definition .
Where a function declaration can occur
According to the description in function Definition and chapter 14th program in the 13th chapter of the ECMAScript specification, functional declarations can only appear in programs (i.e., global environment) or function bodies.
In other words, a function declaration cannot appear in a block (such as an if, while, or for statement).
Problem description
The Tracemonkey engine of Firefox does not handle function declarations as required by the ECMASCRIPT specification, Tracemonkey function declarations in blocks as "function statements". The other browser's engines still parse the function declarations in such blocks as function declarations outside the block.
The impact
Differences in the handling of function declarations in block statements will result in some features not being implemented as expected, or even code errors.
The affected browser
Problem analysis
Tracemonkey handles function declarations in blocks as "function statements." The other browser's engines still parse the function declarations in such blocks as function declarations outside the block.
Analyze the following code:
function foo () { if (window===parent) { function bar () {alert (1);} } else{ function Bar () {alert (2);} } Bar ();} Foo ();
In the above code, two function declarations with the same identifier are placed in the if...else ... Block. This does not conform to the specification, but the methods of handling the engines are not the same.
tracemonkey resolves this function declaration inside a block to a function statement, so that the function statement that can only be executed will take effect, while the other browsers will still treat both as the function declaration of the current scope, that is, whether if...else ... Which branch will eventually be executed, the latter will always overwrite the former, as the function body foo within the identifier of the function of Foo exists.
Note: Firefox in If...else ... The parsing method of using function declarations in the MDC is also described in, see: Conditionally Defining a function.
Assuming that the above code determines the condition window===parent to True, the output results under each browser, as shown in the following table:
| Firefox |
Other Browsers |
| 1 |
2 |
The Note section of the ECMAScript Specification, 5th edition, 12, mentions that although some implementations can handle function declarations as statements , this is not advocated.
Note: Part of this article refers to the article: the content in the quest for a named function expression.
Solution Solutions
Replace the function declaration in a conditional statement with a function expression, such as:
function foo () { if (window===parent) { var bar=function () {alert (1);} } else{ var bar=function () {alert (2);} } Bar ();} Foo ();
See Knowledgebase
Related issues
Test environment
| Operating system version: |
Windows 7 Ultimate Build 7600 |
| Browser version: |
IE6 IE7 IE8 Firefox 3.6 Chrome 4.0.302.3 Dev Safari 4.0.4 Opera 10.51 |
| Test page: |
... |
| This article was updated when: |
2010-07-09 |
Key words
function declaration function expression statement block with the same name function conditionally defining a function
Reproduced
http://w3help.org/zh-cn/causes/SJ9002
Firefox's handling of function declarations within conditional judgment blocks differs from other browsers