Confused about hoisting

前端 未结 5 1442
感情败类
感情败类 2021-01-21 03:09

consider these slightly two different versions of hoisting...

mylocation = \"dublin\" 
function outputPosition() {
    alert(mylocation);
    mylocation = \"fing         


        
5条回答
  •  星月不相逢
    2021-01-21 03:26

    In the second option, you hide mylocation (which I hope was declared in outer scope.) with a new variable via the var declaration.

    "In JavaScript, variable can be declared after being used." meaning: JavaScript pulls up var declarations to the top of the scope(No matter where it was declared!), so in your second function var mylocation is implicitly defined but not assigned before the first alert, hence it output undefined at that point.

提交回复
热议问题