js hoisting

1.变量提升

var x = 2;

function test(){
  console.log(x)
  var x = 1;
}

==》运行程序报错,在test()函数中,x被提升到了顶部声明,相当于

var x = 2;

function test(){
    var x;
  console.log(x)
  x = 1;
}

2.函数提升

a)函数声明可以提升

test();
function test(){
  console.log(123);          
}

b)函数表达式不能提升

test();
var test = function(){
  console.log(123);          
}
原文地址:https://www.cnblogs.com/zmc-change/p/6554126.html