js预解析分段的问题的局部作用域的问题

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>$永远的24k纯帅$</title>
  <script>
    //预解析中,变量的提升,只会在当前的作用域中提升,提前到当前的作用域的最上面
    //函数中的变量只会提前到函数的作用域中的最前面,不会出去
    //预解析会分段(多对的script标签中函数重名,预解析的时候不会冲突)

//    function f1() {
//
//      console.log(num);//undefined
//      var num=10;
//    }
//    f1();
//    console.log(num);//
    function f1() {
      console.log("哈哈");
    }

  </script>
  <script>
    f1();
    function f1() {
      console.log("嘎嘎");
    }
  </script>
</head>
<body>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>$永远的24k纯帅$</title>

  <script>

    //    var a = 25;
    //    function abc (){
    //      alert(a);//undefined
    //      var a = 10;
    //    }
    //    abc();
    //    console.log(a);//25


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

//    var a;
//    a = 18;
//    function f1() {
//      var b;
//      var a;
//      b = 9;
//      console.log(a);//undefined
//      console.log(b);//9
//     a = '123';
//    }
//    f1();


//    function f1() {

var a=b=c=9;
//      var a;//局部变量
//      a=9;
//      //隐式全局变量
//      b=9;
//      c=9;
//      console.log(a);//9
//      console.log(b);//9
//      console.log(c);//9
//    }
//    f1();
//    console.log(c);//  9
//    console.log(b);// 9
//    console.log(a);//报错

    //console.log(parseInt(Math.random()*57+1));
  </script>
  <script>

    f1();//-----报错
   var f1=function () {
      console.log(a);
      var a=10;
    };

    function f2() {

    }
    f2();

  </script>
</head>
<body>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_34412985/article/details/86033079