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);          
}

 

posted @ 2017-03-15 14:42  zmc_change  阅读(99)  评论(0编辑  收藏  举报