javascript学习day06——预解析分段问题和局部作用域问题

1     //1.
2     function f1(){
3         console.log(num);//undefined
4         var num=10;
5     }
6     f1();
7     conosle.log(num);//报错 说明num变量的提升只在当前的作用域中提升,提前到当前的作用域的最上面
8     //函数里的变量不会提到函数外面

1.说明num变量的提升只在当前的作用域中提升,提前到当前的作用域的最上面,函数里的变量不会提到函数外面

 1  <script> //2.
 2    // f2();//哈哈
 3     function f2(){
 4         console.log("哈哈");
 5     }
 6     f2();//哈哈
 7 </script>
 8 <script>
 9     //f2();//嘎嘎
10     function f2(){
11         console.log("嘎嘎");
12     }
13     //f2();//嘎嘎

2.预解析会分段解析,多对的script标签里的函数重名,不会冲突

   var a = 25;
    function f3(){
        alert(a);//undefined
        var a = 10;
    }
    f3();

以上代码相当于

   var a = 25;
    function f3(){
        var a;
        alert(a);//undefined
        a = 10;
    }
    f3();

3.

1     console.log(a);
2     /*输出结果为ƒ a(){
3         console.log('aaaaa');
4     }*/
5     function a(){
6         console.log('aaaaa');
7     }
8     var a=1;
9     console.log(a);//1

以上代码相当于

   var a;
     function a(){
        console.log('aaaaa');
    }   
     console.log(a);
    a=1;
    console.log(a);//1

其他一些值得一看的案例

原文地址:https://www.cnblogs.com/xyishere/p/13672577.html