In JavaScript, function is the first type of JavaScript. When we write down a function, we actually create a function-type entity.
Just as we can write in this form:
FunctionHello () {alert ("Hello");} Hello ();VaRHello =Function() {Alert ("Hello");} Hello ();
They are all the same.
However, when we modify the functions, we will find a strange problem.
<ScriptType= "Text/JavaScript"> FunctionHello () {alert ("Hello");} Hello ();FunctionHello () {alert ("Hello World");} Hello ();</Script>
We can see this result: two consecutive Hello World messages are output. Instead of hello and Hello world.
This is because Javascript is not fully interpreted and executed in order, but a "pre-compilation" of JavaScript will be performed before the interpretation. During the pre-compilation process, the defined function is preferentially executed, and all var variables are created. The default value is undefined to improveProgramExecution efficiency. That is to say, the above sectionCodeIn fact, it is translated into this form by the JS engine pre-Editor:
<ScriptType= "Text/JavaScript"> VaRHello =Function() {Alert ("Hello");} Hello =Function() {Alert ("Hello World");} Hello ();</Script>
We can clearly see from the code above that the function is actually data and a variable. We can also assign values to the function ). Of course, to prevent such a situation, we can also do the following:
<ScriptType= "Text/JavaScript"> FunctionHello () {alert ("Hello");} Hello ();</Script> <ScriptType= "Text/JavaScript"> FunctionHello () {alert ("Hello World");} Hello ();</Script>
In this way, the program is divided into two sections, and the JS engine will not put them together.