console.log(a); var a = 1; var getNum = function(){ a = 2; } function getNum(){ a = 3; } console.log(a); getNum(); console.log(a);
输出结果是 underfined, 1, 2
因为实际代码里面 a被提升在顶部声明了,而且函数声明先于变量申明;所以实际运行里面代码是这样的
var a; console.log(a); //underfined var a = 1; function getNum(){ a = 3; } var getNum = function(){ a = 2; } console.log(a); //1 getNum(); console.log(a); //2