Still the same, start with the code:
<script> <br>var f = function g() { <br>return 1; <br>}; <br>if (false) { <br>f = function g(){ <br>return 2; <br>}; <br>} <br>alert(g()); // 2 <br></script>
Put this code into IE 6 and it will be completely different from Chrome. Two effects.
The output 2 here is the effect in ie6. If it is in Chrome, it will appear that g is not defined.
This can be considered a JScript bug.
It is obvious here that what is here is just the function expression that defines g. Included in the if conditional statement, only the function expression is defined, and the function is not declared.
Then such direct access will definitely be wrong.
So what is a statement and what is a function expression?
In ECMAScript, the two most common ways to create functions are function expressions and function declarations. The difference between the two is a bit confusing, because the ECMA specification only makes one point clear: function declarations must be marked (Identifier) (which is what everyone often calls the function name), and the function expression can omit this identifier:
Function declaration:
function function name (parameters: optional) {function body}
Function expression:
Function function name (optional) (parameter: optional) {function body}
So, it can be seen that if the function name is not declared, it must be an expression, but if If a function name is declared, how to determine whether it is a function declaration or a function expression? ECMAScript differentiates by context. If function foo(){} is part of an assignment expression, it is a function expression. If function foo(){} is contained within a function body, or is located in the program At the top, it's a function declaration.
There is also a less common function expression, which is the one enclosed by parentheses (function foo(){}). The reason why it is an expression is because the parentheses () are a grouping operator. It can only contain expressions inside.
You may think that when using eval to execute JSON, the JSON string is usually enclosed in parentheses: eval('(' json ')'). The reason for this is because The grouping operator, that is, this pair of parentheses, will force the parser to parse the JSON curly braces into expressions instead of code blocks.