• 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.

     

      

  • 相关阅读:
    一种Hive性能调优方法(补充)
    Leetcode之MySQL练习题
    Hive的10种优化总结
    SQL练习题(1-76)学习笔记(来源于牛客网)
    SQL练习题47:将employees表中的所有员工的last_name和first_name通过(')连接起来
    SQL练习题46:牛客每次考试完,都会有一个成绩表(grade),请你写一个sql语句查询各个岗位分数升序排列之后的中位数位置的范围,并且按job升序排序
    SQL练习题45:
    SQL练习题44:牛客每天有很多人登录,请你统计一下牛客每个日期新用户的次日留存率。
    CDN-jQuery
    [转]vue和微信小程序的区别、比较
  • 原文地址:https://www.cnblogs.com/wengXiaofeng/p/JavaScript.html
Copyright © 2020-2023  润新知