JavaScript的预编译

一、预编译

1、检查通篇的语法错误
2、预编译的过程

函数声明整体提升。
变量只有声明提升,赋值不会提升。
3、解释一行,执行一行

二、暗示全局变量

未被声明就赋值了的话,那么该变量就是全局变量,所有权归window

        a = 2
        console.log(a);//2
        document.writeln(window.a)//2
   function test() {
            var a = b = 1
        }
        test()
        //console.log(a); //a is not defined
        console.log(b);//1
        console.log(window.a);//undefined
        console.log(window.b);//1
    console.log(a());//2   undefined
        var a = 1
        function a() {
            console.log(2); //1   2
        }
        console.log(a);//3   1
      console.log(a);//ƒ a(a) {...}
        function a(a) {
            var a = 10
            var a = function () {

            }
        }
        var a = 1
三、预编译的过程:

例子1:

function test(a) {
            console.log(a);  //ƒ a() { }
            var a = 1
            console.log(a); //1 
            function a() { }
            console.log(a);//1
            var b = function () { }
            console.log(b);//ƒ () { }
            function d() { }
        }
        test(2)

创建 AO activation object 活跃对象,函数上下文。
AO={
第一步:寻找函数的形参和变量声明
a=undefined
b=undefined
第二步:把实参的值赋值给形参
a=undefined -> 2
b=undefined ->
第三步:寻找函数声明赋值给函数体
a=undefined -> 2 -> function a() { }
b=undefined -> -> function () { }
d=function d() { }
第四步:逐行执行
b=undefined -> -> -> function () { }
}
例子2:
第一步:找形参和变量声明
a=undefined
b=undefined
c=undefined
第二步:把形参给实参
a=undefined -> 1
b=undefined
c=undefined
第三步:把函数声明给函数体
因为声明了:function b() { }和 function d() { }
所以:
a=undefined -> 1 ->
b=undefined -> -> function b() { }
c=undefined -> ->
function d() { }
第四步:从上到下逐行执行
a=undefined -> 1 -> -> 1
b=undefined -> -> function b() { } -> 6
c=undefined -> -> -> 6
function d() { }

 function test(a, b) {
            console.log(a); //1
            c = 0;
            var c;
            a = 5
            b = 6
            console.log(b);  //6
            function b() { }
            function d() { }
            console.log(b);//6
        }
        test(1)

例子3:

        a = 1
        function test() {
            console.log(a);//undefined
            a = 2
            console.log(a);//2
            var a = 3
            console.log(a);//3
        }

        test()
        var a

        // GO={
        //     a:undefined
        //       1
        //       test:function test(){}
        // }
//AO 有a就不会去GO找
        // AO={
        //     a:undefined
        //     2
        //     3
        // }

例子4:

        function test() {
            console.log(b); //undefined
            if (a) {
                var b = 2
            }
            c = 3//相当于在全局声明变量c
            console.log(c);//3
        }
        var a;
        test();
        a = 1
        console.log(a);//1

// GO={
//     a=undefined
//    test:f test() {}
//       1
// }
// AO={
//     b=undefined
//     c=3  全局
//     undefined
//     3
    
// }

例子5、

        // 1. return 的作用:返回一个值到函数外部,2.终止函数
        function test() {
            return a
            a = 1
            function a() { }
            var a = 2
        }
        console.log(test()); //ƒ a() { }

// AO={
//     a=undefined   -> function a() { }
// }

例子6、

        a = 1
        function test(e) {
            function e() { }
            arguments[0] = 2
            console.log(e);//2
            if (a) {//a 是undefined,所以返回false
                var b = 3
            }
            var c;
            a = 4
            var a;
            console.log(b);//undefined
            f = 5
            console.log(c);//undefined
            console.log(a);//4

        }
        var a;
        test(1)
        console.log(a);//1
        console.log(f);//5

        // GO={
        //     a=undefined  ->  1
        //     test:function test(){}
        // }

        // AO={
        //     e:undefined   ->   1   ->  function e() { }   ->    2
        //     b:undefined
        //     c:undefined
        //     a:undefined   ->  4
        // }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文章目录 JavaScript运行三部曲 JavaScript预编译 01 关于预编译的一些知识点 02 预编译四...
    寒羽鹿阅读 505评论 1 0
  • ######预编译前奏 1.imply global 暗示全局变量:即任何变量未经声明就赋值,则此变量就为全局对象...
    branv阅读 134评论 0 0
  • js运行三部曲 语法分析 预编译 解释执行 预编译前奏 imply global 暗示全局变量, 即任何变量, 如...
    刘翾阅读 579评论 0 1
  • 大家要明白,这个预编译和传统的编译是不一样的(可以理解js预编译为特殊的编译过程)JavaScript是解释型语言...
    pengtoxen阅读 310评论 0 0
  • 今天用了大量时间复习了作用域、预编译等等知识看了很多博文,翻了翻以前看过的书(好像好多书都没有讲预编译)发现当初觉...
    最爱喝龙井阅读 242评论 0 0