js代码的执行分为两个步骤函数
1.预解析code
提高(hoisting) JavaScript代码在预解析阶段,会对以var声明的变量名,和function开头的语句块,进行提高操做
2.执行ip
func(); function func(){ alert("Funciton has been called"); } //变量的提高 alert(a); var a = 1; //提高以后的代码模拟 var a; alert(a); a = 1;
函数同名,如何提高ci
预处理的时候,会将两个函数所有提高,可是后面的函数会覆盖掉前面函数it
func1(); //last function func1(){ console.log('This is first func1'); } func1(); //last function func1(){ console.log('This is last func1'); } //预解析提高后的代码 function func1(){ console.log('This is first func1'); } function func1(){ console.log('This is last func1'); } func1(); //last func1(); //last
变量和函数同名io
在提高的时候,若是有变量和函数同名,会忽略掉变量,只提高函数console
alert(foo); //undefined 函数体 function foo(){} var foo = 2; alert(foo); //2 //预解析 提高后的代码 function foo(){}; alert(foo); foo=2; alert(foo);