美文网首页
js questions

js questions

作者: 迪爷 | 来源:发表于2017-04-10 13:15 被阅读0次

    1. What will be the output of the following code:

    for (var i = 0; i < 5; i++) {
      setTimeout(function() { console.log(i); }, i * 1000 );
    }
    

    Explain your answer. How could the use of closures help here?

    The code sample shown will not display the values 0, 1, 2, 3, and 4 as might be expected; rather, it will display 5, 5, 5, 5, and 5.
    The reason for this is that each function executed within the loop will be executed after the entire loop has completed and all will therefore reference the last value stored in i, which was 5.
    Closures can be used to prevent this problem by creating a unique scope for each iteration, storing each unique value of the variable within its scope, as follows:

    for (var i = 0; i < 5; i++) {
        (function(x) {
            setTimeout(function() { console.log(x); }, x * 1000 );
        })(i);
    }
    

    This will produce the presumably desired result of logging 0, 1, 2, 3, and 4 to the console.

    2. A closure

    A closure is an inner function that has access to the variables in the
    outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables.

    Here is a simple example:

    var globalVar = "xyz";
    
    (function outerFunc(outerArg) {
    var outerVar = 'a';
    
    (function innerFunc(innerArg) {
      var innerVar = 'b';
      
      console.log(
        "outerArg = " + outerArg + "\n" +
        "innerArg = " + innerArg + "\n" +
        "outerVar = " + outerVar + "\n" +
        "innerVar = " + innerVar + "\n" +
        "globalVar = " + globalVar);
      
    })(456);
    })(123);
    

    In the above example, variables from innerFunc, outerFunc, and the global namespace are all in scope in the innerFunc. The above code will therefore produce the following output:

    outerArg = 123
    innerArg = 456
    outerVar = a
    innerVar = b
    globalVar = xyz
    

    3

    splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。
    注释:该方法会改变原始数组。
    splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。

    push()
    push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。
    它直接修改 arrayObject,而不是创建一个新的数组。push() 方法和 pop() 方法使用数组提供的先进后出栈的功能。

    reverse() 方法用于颠倒数组中元素的顺序。
    该方法会改变原来的数组,而不会创建新的数组。

    slice() 方法可从已有的数组中返回选定的元素。
    返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。

    sort() 方法用于对数组的元素进行排序。
    返回值
    对数组的引用。请注意,数组在原数组上进行排序,不生成副本。
    sort 该方法会改变原来的数组,而不会创建新的数组。

    concat() 方法用于连接两个或多个数组。
    该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。

    4 typeof

    undefined 值未定义
    boolean 布尔值
    string 字符串
    number 数值
    object 对象或 null
    function 函数

    4.1 undefined

    只有一个值
    对未初始化的变量执行 typeof 操作符会返回 undefined 值,而对未声明的变量执行typeof 操作符也会返回 undefined 值

    var message;
    alert(typeof message);// undefined
    alert(typeof age); // undefined
    

    这个结果有其逻辑上的合理性,无论对哪种变量也不可能执行真正的操作。

    4.2 Null

    第二个只有一个值的数据类型。

    相关文章

      网友评论

          本文标题:js questions

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