zoukankan      html  css  js  c++  java
  • Javascript interview knowledge points

    1、variable hoist

    Q:What's the output?

    function sayHi() {
      console.log(name);
      console.log(age);
      var name = "Lydia";
      let age = 21;
    }
    
    sayHi();
    • A: Lydia and undefined
    • B: Lydia and ReferenceError
    • C: ReferenceError and 21
    • D: undefined and ReferenceError

    Answer: find yourself

    Within the function, we first declare the name variable with the var keyword. This means that the variable gets hoisted (memory space is set up during the creation phase) with the default value of undefined, until we actually get to the line where we define the variable. We haven't defined the variable yet on the line where we try to log the name variable, so it still holds the value of undefined.

    Variables with the let keyword (and const) are hoisted, but unlike var, don't get initialized. They are not accessible before the line we declare (initialize) them. This is called the "temporal dead zone". When we try to access the variables before they are declared, JavaScript throws a ReferenceError.

    2、scope

    Q: What's the output?

    for (var i = 0; i < 3; i++) {
      setTimeout(() => console.log(i), 1);
    }
    
    for (let i = 0; i < 3; i++) {
      setTimeout(() => console.log(i), 1);
    }
    • A: 0 1 2 and 0 1 2
    • B: 0 1 2 and 3 3 3
    • C: 

    Answer: find yourself

    Because of the event queue in JavaScript, the setTimeout callback function is called after the loop has been executed. Since the variable i in the first loop was declared using the var keyword, this value was global. During the loop, we incremented the value of i by 1 each time, using the unary operator ++. By the time the setTimeout callback function was invoked, i was equal to 3 in the first example.

    In the second loop, the variable i was declared using the let keyword: variables declared with the let (and const) keyword are block-scoped (a block is anything between { }). During each iteration, i will have a new value, and each value is scoped inside the loop.

    3. What's the output?

    const shape = {
      radius: 10,
      diameter() {
        return this.radius * 2;
      },
      perimeter: () => 2 * Math.PI * this.radius
    };
    
    console.log(shape.diameter());
    console.log(shape.perimeter());
    • A: 20 and 62.83185307179586
    • B: 20 and NaN
    • C: 20 and 63
    • D: NaN and 63

     

    Answer: 

    Note that the value of diameter is a regular function, whereas the value of perimeter is an arrow function.

    With arrow functions, the this keyword refers to its current surrounding scope, unlike regular functions! This means that when we call perimeter, it doesn't refer to the shape object, but to its surrounding scope (window for example).

    There is no value radius on that object, which returns undefined.

    4. What's the output?

    console.log(+true)
    console.log(!"Lydia")
    • A: 1 and false
    • B: false and NaN
    • C: false and false

      

    Answer: 

    The unary plus tries to convert an operand to a number. true is 1, and false is 0.

    The string 'Lydia' is a truthy value. What we're actually asking, is "is this truthy value falsy?". This returns false.

     

      

  • 相关阅读:
    【51NOD】1135 原根
    RuntimeWarning: More than 20 figures have been opened.
    获取当前文件夹下文件个数
    U盘装win10(我们无法创建新的分区,也找不到现有分区)
    我们无法创建分区也找不到现有的分区
    装系统——出现“invalid partition table”提示的原因
    python帮助文档的浏览
    opencv-python ,CV2读取中文路径和中文名称图片无结果,解决办法
    pyx文件编译
    安装polygon
  • 原文地址:https://www.cnblogs.com/wengXiaofeng/p/JavaScript.html
Copyright © 2011-2022 走看看