In this article, a Bug in FireFox and FireBug has happened to discover the function declaration problem in FireFox and has never been solved by authoritative materials. When interviewing Sina Weibo in November 26, the interviewer also asked this question. Of course, he did not read my BLOG and learned from him that this is not a BUG, but the MongoDB engine of FF is resolved in this way, of course, my answer is: "I guess that the function declaration in the FF statement is converted into a function expression ". The interviewer said no. Of course, no specific answer was provided, prompting me to say that MDN has such information.
If you are too lazy to read the previous article, you can take a look at the following simple example. Run it all over FF/IE/Chrome to see the difference between FF and the other two: (FF will report an error, other function code will pop up)
View plain
If (0 ){
Function (){}
}
Alert ();
In the evening, I went to find the MDN, which was found by me. From the data point of view, it was consistent with my speculation, the example is similar to the article I mentioned earlier. The specific link is as follows:
Https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression
The English level is not very good. Here I will post some important parts and examples and translate them a little (I strongly suggest reading the original article. My translation is too bad ):
Function declarative expressions are very easy (and often unexpected) to convert to function expressions. A function declaration is either converted into an expression part, or is no longer a function or the "source element" of the script itself ". "Source element" is a non-nested statement in a script or a function.
The following is an example:
Var x = 0; // source element
If (x = 0) {// source element
X = 10; // not a source element
Function boo () {}// not a source element
}
Function foo () {// source element
Var y = 20; // source element
Function bar () {}// source element
While (y = 10) {// source element
Function blah () {}// not a source element
Y ++; // not a source element
}
}
Examples:
// Function declaration
Function foo (){}
// Function expression
(Function bar (){})
// Function expression
X = function hello (){}
If (x ){
// Function expression
Function world (){}
}
// Function declaration
Function (){
// Function declaration
Function B (){}
If (0 ){
// Function expression
Function c (){}
}
}
Condition defines a function
A Function can be defined using a // Function statement // (an extension allowed by an ECMA-262 version 3rd) or a Function to construct a Function condition.
In the following script, the zero function will never be defined and cannot be triggered, Because 'if (0) 'judges its condition as false:
If (0 ){
Function zero (){
Document. writeln ("This is zero .");
}
}
If the condition for changing the script is 'if (1) ', the function zero is defined.
Note: although such a function looks like a function declaration, it is essentially a statement because it is nested in another statement. See the differences between the function description and the function expression.
Note: Some JavaScript Engines, excluding SpiderMonkey (ff js engine), incorrectly regard any function name expression as a function declaration. This will lead to zero being defined, and even when conditions are always false. A safe way to define a conditional function is to define an anonymous function and assign it to a variable ):
If (0 ){
Var zero = function (){
Document. writeln ("This is zero .");
}
}
Summary
MDN is a valuable material. If you have time, you really need to check it out.
From the Exodia Column