js预编译

发布于:2022-07-26 ⋅ 阅读:(237) ⋅ 点赞:(0)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //预编译:代码解释之前
        //全局预编译
        //第一步创建Go对象(global object)
        //第二步找变量声明,将变量声明作为GO对象的属性值传进去,赋值为undefined。
        //第三部找函数声明(不要找函数表达式),赋值于对应的Go属性值。


        // var a = 10;
        // var c = "hello";
        // b();
        // function b() { }  //声明提前
        // var d = function () { }
        // d()

        // 伪代码   预编译
        // GO{
        //     a: undefined,
        //     c: undefined,
        //     d: undefined,
        //     b: function b() { },
        // }

        // 代码执行后:
        // a: 10,
        // c: "hello",
        // d: function () { },
        // b: function b() { },


        // 局部预编译
        //局部预编译
        //1.创建Ao(Active object)对象
        //2.找函数的形参和变量声明,将变虽声明和形参作为Ao属性名,值为undefined
        //3.将实参值和形参统一
        //4.在函数休里面找函数声明(只找函数声明,不找函数表达式),值赋予函数体

 // function box(a, b) {
    //     console.log(a, b);
    //     var c = 100;
    //     function d() { }
    //     d()
    // }
    // box(1, 2)

    // 伪代码
    // AO{
    //     a:1,
    //     b:2,
    //     c:undefined,
    //     d:function d(){}
    // }

    // var a = 10;
    // function box(a) {
    //     a = 100;
    //     console.log(a);
    //     var a;
    // }
    // box(10);


    var a = 10;
    function a() { }
    a();

    // GO{
    //     a: function a(){},
        
    // }

    // 代码执行后:
    // a:10

    // AO{
    //     a:100,
    // }


    // var a = 10;
    // function box() {
    //     a = 100;
    //     console.log(a);
    //     var a;
    // }
    // box();

    // GO{
    //     a:undefined
    // }

    // 运行后 全局
    // {
    //     a:10
    // }

    // AO{
    //     a: undefined
    // }

    // 函数里代码执行
    // a:100

    // js执行

    // a: 10,
    // c: "hello",
    // b:function b(){}


        //作用域:变量起作用的范围
        // 1.全局作用域
    // 2.局部作用域(函数作用域)

    // var a = 10;//全局变量
    // console.log(a);//10
    // function box() {
    //     var b = "hello";//局部变量
    // console.log(a);//10
    // console.log(b);
    // }

    // box();

    // console.log(b);


    // var a = 10;//全局变量
    // function box() {
    //     console.log("函数内:" + a);
    // }

    // console.log("函数外:" + a);
    // box();


    // var a = 10;//全局变量

    // function box() {
    //     a = 100;
    //     console.log("函数内:" + a);
    // }
    // console.log();//10
    // box();//100

    // box();//100
    // console.log("函数外:" + a);//100

    // 在函数外【声明】的变量 我们称为全局变量
    // 在函数内【声明】的变量 我们称为局部变量

    // function box() {
    //     var a = 100;//局部变量
    //     console.log("函数内:" + a);
    // }
    // box();//100
    // console.log(a);//报错

    // var a = 10;//全局变量
    // function box() {
    //     var a = 100;//局部变量
    //     console.log("函数内:" + a);
    // }
    // box();//100
    // console.log(a);//10

    // a = 10;//变量未声明直接使用,默认自动为全局变量
    // function box() {
    //     a = 100;
    //     console.log("函数内:" + a);
    // }
    // box();//100
    // console.log(a);//100

    // var 声明变量 会进行声明提升

    // var a = 10;
    // function box() {
    //     console.log("函数内:" + a);//先在自己的作用域里查找这个变量,如果找到就是该值,如果找不到,去上级作用域里查找...
    //     // var a;
    //     var a = 100;//声明提前  赋值不提前
    // }
    // box();
    // console.log(a);

    // var a = 10;
    // function box() {
    //     a = 100;
    //     console.log("函数内:" + a);//先在自己的作用域里查找这个变量,如果找到就是该值,如果找不到,去上级作用域里查找...
    //     var a;
    // }
    // box();
    // console.log(a);

    // 函数作用域
    </script>
</head>

<body>

</body>

</html>