js hoisting

1.變量提高函數

var x = 2;

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

==》運行程序報錯,在test()函數中,x被提高到了頂部聲明,至關於spa

var x = 2;

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

2.函數提高code

a)函數聲明能夠提高blog

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

b)函數表達式不能提高io

test();
var test = function(){
  console.log(123);          
}
相關文章
相關標籤/搜索