js 变量作用域

 

例子

<script>
    var a = "heh"
    function findLove(){
     console.log(a);
     function findforyou(){
         var a ="you";
         console.log(a);
     }
     function findother(){
         console.log(a)
     }
     findforyou();
     findother();
    }
    findLove();
</script>

输出

heh
you
heh

 

例子

<script>
 var test_id = "my love";
  if(true){
      console.log(test_id);
      var test_id = "where my love?";
      console.log(test_id);
  }
  console.log(test_id);
</script>

输出

my love
where my love?
where my love?

 

例子

<script>
   var test_id = "my love";
    function findLove(){
        var test_id ;
        console.log(test_id);
        test_id = "is you?";
        console.log(test_id);
    }
    findLove();
    console.log(test_id);
</script>

输出

undefined
is you?
my love

 

例子

<script>
   var a = "heh"
   if(true){
       console.log(a);
   }
</script>

输出

heh

 

例子

<script>
   var a = "heh"
   function findLove(){
       console.log(a);
   }
   findLove();
</script>

输出

heh

 

例子

<script>
   var a = "heh"
   function findLove(){
       console.log(a);
       var a
   }
   findLove();
</script>

输出

undefined

 

posted @ 2019-04-23 11:23  anobscureretreat  阅读(147)  评论(0编辑  收藏  举报