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.

     

      

  • 相关阅读:
    PreferenceScreen 偏好显示类 的使用
    Android实战技巧:如何在ScrollView中嵌套ListView
    android onActivityResult不执行问题
    java zip解压中文乱码问题
    RandomAccessFile读取大文件时效率很低,现进行改进---BufferedRandomAccessFile
    HTTP RANGE(多线程下载相关)
    Android实现ListView异步加载图片
    自己调用webservice方法总结(带请求头SoapHeader)
    Android将程序崩溃信息保存本地文件
    Android的intent之间复杂参数的传递
  • 原文地址:https://www.cnblogs.com/wengXiaofeng/p/JavaScript.html
Copyright © 2011-2022 走看看