Or the same, first code:
Copy Code code as follows:
<script>
var f = function g () {
return 1;
};
if (false) {
f = function g () {
return 2;
};
}
Alert (g ()); 2
</script>
Throwing this code into IE 6 and chorme is a completely different two-way effect.
Output 2 Here is the effect in IE6, if G is not defined in Chorme.
This is also a JScript bug.
It is obvious here that only the function expression of g is defined here. Included in the conditional statement of if, only the function expression is defined, not the function is declared.
So direct access is definitely going to go wrong.
So what is a declaration and what is a function expression?
In ECMAScript, the two most commonly used methods of creating functions are function expressions and function declarations, and the difference between the two is a bit faint, because the ECMA specification is only a bit clear: the function declaration must have an identifier (Identifier) (the name of the function that everyone often says). And a function expression can omit this designator:
function declaration:
function function name (parameter: optional) {Functional Body}
function expression:
function function name (optional) (parameter: optional) {Functional Body}
So, as you can see, if you don't declare the function name, it's definitely an expression, but if you declare the function name, how do you decide whether it's a function declaration or a function expression? ECMAScript is differentiated by context, and if function foo () {} is part of an assignment expression, it is a function expression, if function foo () {} is contained in a function body, or at the top of the program, So it's a function declaration.
There is also a function expression that is less common and is enclosed in parentheses (function foo () {}) and is an expression because parentheses () is a grouping operator whose interior can only contain expressions.
You might think that when you use Eval for JSON, the JSON string is usually enclosed in parentheses: eval (' (' + JSON + ') '), because the grouping operator, which is the pair of parentheses, Lets the parser force the curly braces of the JSON to parse into an expression instead of a block of code.