zoukankan      html  css  js  c++  java
  • js 面试题总结 3

    1 console.log(a); // undefined
    2 var a = 12; // 12
    3 function fn() {
    4   console.log(a); // undefined
    5   var a = 13; // 13
    6 }
    7 fn();
    8 console.log(a); // 12
    1. 创建全局作用域;
    2. 变量提升,声明一个 a 变量、定义一个函数 fn;
    3. 执行代码;( 变量 a 已提升但未赋值,所以 a = undefined,输出 a 值为 undefined,设置 全局变量 a = 12 )
    4. 执行 fn,形成一个私有作用域 fn ;
    5. 私有作用域中的执行过程为:
      1)形式参数赋值。没有形式参数;
      2)变量提升。提升变量 a ;(私有作用域中,如果定义了变量,那么所有的相同变量名都是同一个)
      3)执行代码。局部变量 a已提升还未赋值,所以是 undefined,输出 a 值为 undefined,设置局部变量 a = 13 ;
    6. 输出全局变量 a 值 为 12;

    1 console.log(a); // undefined
    2 var a = 12; // 12
    3 function fn() {
    4   console.log(a); // 12
    5   a=13; // 13
    6 }
    7 fn();
    8 console.log(a); // 13
    9 // 结果为 undefined 、12 、13
     1 console.log(a); // a is not defined
     2 
     3 a=12;
     4 function fn() {
     5   console.log(a);
     6   a = 13;
     7 }
     8 fn();
     9 console.log(a);
    10 // a is not defined

     1 var foo = 1;
     2 function bar () {
     3     /** 
     4      * 形参赋值:无
     5      * 变量提升 
     6      * var foo 
     7     */
     8     if (!foo) { // ==> !undefined ==> true
     9         var foo = 10;
    10     }
    11     console.log(foo);// foo => foo 
    12 }
     1 var n = 0;
     2 function a() {
     3     var n = 10;
     4     function b() {
     5         n++;
     6         console.log(n);
     7         console.log(this);
     8     }
     9     b();
    10     return b;
    11 }
    12 var c = a();
    13 c();
    14 console.log(n);
    15 
    16 // ---------------------------------
    17 // 创建全局作用域 n、 c、a(a =>> 指向函数堆栈的地址 )
    18 // 开始执行 n = 0; c = a() 此过程会把 a 执行结果赋值给 c 变量); 
    19     // 执行 a() 时候,创建私有 window.a 作用域,有变量 n、 b (b =>> 指向函数堆栈的地址 000111)
    20     // 开始执行 n = 10; b();
    21         // 执行 b() 时候,创建 window.a.b 作用域,变量为 n 取自上级作用域 a.n;
    22         // 执行 n++; console.log(n);( n => 10 + 1; console.log(11);)
    23         // 之后 a.b 作用域会销毁,因为此作用域在其他处未被引用
    24     // return b; (b => 函数堆栈的地址 000111)
    25 // var c = a (); 会把 a 作用域中的 b 值赋给变量 c, 所以 a 作用域不销毁。
    26 // 开始执行 c();
    27 // 形成 c 时候,创建私有作用域,有变量 n 取自上级作用域还是 a.n ; 
    28 // 执行 n++; console.log(n);( n => 0 + 1; console.log(1);)
    29 
    30 // 函数作用域只和在哪里创建有关,和在哪里执行没有关系。
  • 相关阅读:
    flock对文件锁定读写操作的问题 简单
    hdu 2899 Strange Fuction(二分)
    hdu 2199 Can you solve this equation? (二分)
    poj 3080 Blue Jeans (KMP)
    poj 2823 Sliding Window (单调队列)
    poj 2001 Shortest Prefixes (trie)
    poj 2503 Babelfish (trie)
    poj 1936 All in All
    hdu 3507 Print Article (DP, Monotone Queue)
    fzu 1894 志愿者选拔 (单调队列)
  • 原文地址:https://www.cnblogs.com/lvyongbo/p/9938155.html
Copyright © 2011-2022 走看看