There are three ways to define a function: 1,function fun () This is called a function declaration; 2,var fun=function () is called a function expression; 3,var fun=new function () is also called a function expression.
In the same execution environment, the function declaration is promoted to the front of the code, no matter where it is, even if it is preceded by a function expression, this is the JS rule.
Examples:
function Fun () { alert (0); } Fun (), var fun = function () { alert (1); } Fun (), var fun =function () { alert (2); } Fun (), function Fun () { alert (3); } Fun ();
Answer: 3122
The first thing to do is definitely a fun (), which points to the last function fun (). It then executes a second fun (), which points to the first var fun = function (). Then execute the third fun (), the second var fun = function () overrides the first, because JS has no overloaded method, so this is the process of running.
The equivalent of the code becomes this:
function Fun () { alert (0); } function Fun () { alert (3); } Fun (), var fun = function () { alert (1); } Fun (), var fun =function () { alert (2); } Fun (), fun ();
JS definition function