美文网首页
js 预编译 函数作用域

js 预编译 函数作用域

作者: ynwshy | 来源:发表于2021-03-14 22:22 被阅读0次
function fn(a, c) {
        // console.log(ggggg); // Uncaught ReferenceError: ggggg is not defined
        console.log("xx : ", xx);
        console.log("yy : ", yy);
        console.log("zz : ", zz);
        var xx = function () {};
        function yy() {}
        var zz = 666;
        console.log("xx : ", xx);
        console.log("yy : ", yy);
        console.log("zz : ", zz);

        console.log("=================");

        console.log("a : ", a);
        console.log("b : ", b);
        console.log("c : ", c);
        console.log("d : ", d);
        console.log("f : ", f);
        console.log("----------------");

        a();
        // console.log("abc : ", abc); // Uncaught ReferenceError: abc is not defined
        var a = 123;
        console.log("a : ", a);
        function a() {
          console.log("function a() ...");
          console.log("abc - a() : ", abc);
          // 第一次编译期 变量不会提升 调用函数a() 创建a()的AO{}后提升
          var abc = 999;
        }
        console.log("a : ", a);
        // a(); Uncaught TypeError: a is not a function
        if (false) {
          var d = 678; // 编译期 变量也会提升
        }
        for (let index = 0; index < 10; index++) {
          var f = index; // 编译期 变量也会提升
        }
        var b = function () {};
        function c() {}
        console.log("----------------");
        console.log("a : ", a);
        console.log("b : ", b);
        console.log("c : ", c);
        console.log("d : ", d);
      }
      fn(1, 2);
xx :  undefined
yy :  ƒ yy() {}
zz :  undefined
xx :  ƒ () {}
yy :  ƒ yy() {}
zz :  666
=================
a :  ƒ a() {
    console.log("function a() ...");
    console.log("abc - a() : ", abc);
    var abc = 999;
}
b :  undefined
c :  ƒ c() {}
d :  undefined
f :  undefined
----------------
function a() ...
abc - a() :  undefined
a :  123
a :  123
----------------
a :  123
b :  ƒ () {}
c :  ƒ c() {}
d :  undefined
编译期 1.形参和变量声明赋值undefined -> 2.形参赋值 -> 3.函数声明赋值
AO{
    --------------|-----|------------------|
    a: undefined  |  1  | function a() {}  |
    c: undefined  |  2  | function c() {}  |
    b: undefined  |     |                  |
    d: undefined  |     |                  |
}

相关文章

  • 成长(4/2000)——面试题合集1

    Javascript 1.作用域 概念: 作用域分全局作用域和函数作用域 预编译 js的两个阶段,预编译和解释执行...

  • 变量作用域

    变量作用域:静态作用域、动态作用域JS变量作用域:JS使用静态作用域JS没有块级作用域(全局作用域、函数作用域等)...

  • 你不懂的js上卷(一)

    基本概念 js引擎 编译器 作用域变量赋值操作 词法作用域(定义词法阶段的作用域) 1.由变量或者函数声明时的...

  • JS的作用域

    JS的作用域: 全局作用域、函数作用域、eval 作用域、块级作用域 全局作用域: 函数作用域: 结果截屏: 说...

  • 你不知道的JS-上卷

    作用域整体理解:JS作用域分为函数作用域,全局作用域,with和try catch形成的块级作用域。 JS引擎在编...

  • js作用域

    词法作用域 -js采用的是词法作用域,函数的作用域基于函数创建的位置。

  • 闭包

    js的作用域分为全局作用域,和函数作用域, 内部作用域可以访问外部作用域的函数参数,外部不能访问内部函数的变量和参...

  • 作用域链&闭包&函数相关

    作用域链 在JS中函数可以创建作用域; 函数中又可以创建函数(可以开辟新的作用域); 函数内部的作用域可以访问外部...

  • js 闭包

    一、js 作用域 讲闭包首先就要理解 js 的作用域。再 ES5 中,js 有两种作用域,全局作用域和函数作用域(...

  • js 的变量提升和函数提升

    1. 深入理解 js 的变量提升和函数提升 先了解:js没有块级作用域,只有全局作用域,和函数作用域 相同的函数名...

网友评论

      本文标题:js 预编译 函数作用域

      本文链接:https://www.haomeiwen.com/subject/uopqcltx.html