var和function的预解析,以及变量和function的先后顺序的问题

 function b() {
             console.log(a);// function a(){}
             var a = 10;
             function a() { };
             a = 100;
             console.log(a);//100
         }
         b();
 
 
         function c() {
             console.log(a);//function a(){}
             function a() { };
             var a = 10;
             a = 100;
             console.log(a);//100
         }
         c();
 
 
         (function d(num) {
             console.log(num);//100
             var num = 10;
         }(100))
 
 
             (function e(num) {
                 console.log(num);//function num () {};
                 var num = 10;
                 function num() { };
             }(100))
 
 
             (function f(num) {
                 function num() { };
                 console.log(num);//function num () {};
                 var num = 10
                 console.log(num);//10
             }(100))
  • 仍然是预解析(在与解析过程中还要考虑一下当前变量的作用域)
 function m() {
            console.log(a1); // underfined
            console.log(a2); // underfined
            console.log(b1); // underfined
            console.log(b2); // underfined
            if (false) {
                function b1() { };
                var a1 = 10;
            }
            if (true) {
                function b2() { };
                var a2 = 10;
            }
            console.log(a1); // underfined
            console.log(a2); // 10
            console.log(b1); // underfined
            console.log(b2); // function
        }
        m();
        function n() {
            if (2 > 1) {
                arr = 10;
                brr = 10;
                let arr;//要在严格模式'use strict'下运行
                var brr;
                console.log(arr);
                console.log(brr);
            }
        }
        n(); // ReferenceError
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页