21个js面试基础题

From 21个js面试基础题

  1. undefined 和 not defined区别?

    当尝试使用不存在并且没有声明的变量时,JavaScript会抛出 not defined 的错误,但是使用typeof时会返回undefined。
    如果一个变量既没有声明也没有定义,那么会返回not defined;
    如果声明了但是没有定义,那么会返回undefined。

  1. 输出什么

    var y = 1; 
    if (function f(){}) { 
        y += typeof f; 
    } 
    console.log(y); 
    

    答案: 1undefined

    if相当于eval(),eval(function f(){})返回true,而返回undefined的原因是当(function f(){})执行时,if内部的代码已经进行了typeof f的判断。
    (because the if statement code executes at run time, and the statement inside the if condition is evaluated during run time.)

    var k = 1;  
    if (1) {  
        eval(function foo(){});  
        k += typeof foo;  
    } 
    console.log(k);   
    //依然输出1undefined
    
    var k = 1;  
    if (1) {  
        function foo(){};  
        k += typeof foo;  
    }
    console.log(k); 
    // output 1function
    
  2. 创建私有方法的缺点是什么?

    内存浪费,每new一个对象都需要为该方法分配一个内存

  3. 什么是闭包?

    能够引用另一个函数作用域变量的函数。
    A closure is a function defined inside another function (called the parent function), and has access to variables that are declared and defined in the parent function scope.

  4. 写出mul函数

    console.log(mul(2)(3)(4)); // output : 24     
    console.log(mul(4)(3)(4)); // output : 48  
    

    答案:

    function mul (x) {    
        return function (y) { // anonymous function  
            return function (z) { // anonymous function     
                return x * y * z;     
            };    
        };    
    }    
    
  5. 如何清空一个数组?

    • 当没有别的地方引用时,可直接arr = [],否则引用的地方还是无法清空。
    • arrayList.length = 0; 该方法可以使其他引用该数组的地址也一起清空。
    • arrayList.splice(0, arrayList.length);
    • while(arrayList.length){
      arrayList.pop();  
      
      }
  6. 如何检查一个对象是不是数组?

    • Object.prototype.toString.call(arr)
    • $.isArray (JQuery)
    • Array.isArray (Chrome 5, Firefox 4.0, IE 9, Opera 10.5 and Safari 5)
  7. 输出

    var output = (function(x){  
        delete x;  
        return x;  
    })(0);  
    
    console.log(output);  //output 0
    

    delete 是用来删除一个对象的属性,x不是对象

  8. 输出

    var x = 1;
    var output = (function(){
        delete x;
        return x;
    })();
    
    console.log(output);
    //output 1
    
  9. var x = { foo : 1};  
    var output = (function(){  
        delete x.foo;  
        return x.foo;  
    })();   
    
    console.log(output); 
    
    //output undefined  
    
  10. var Employee = {
    company: ‘xyz’
    }
    var emp1 = Object.create(Employee);
    delete emp1.company
    console.log(emp1.company);

    //output xyz
    emp1中company属性是在原型链上的,delete不能删除原型链上的属性,可以直接delete Employee.company,或者delete emp1.proto.company

  11. 什么是undefined x 1?

    var trees = ["redwood","bay","cedar","oak","maple"];
    delete trees[3];
    
    //output ["redwood", "bay", "cedar", undefined × 1, "maple"]
    

    Chrome对于数组中未初始化的位置的展示方式

  1. output?

    var trees = [“xyz”,”xxxx”,”test”,”ryan”,”apple”];
    delete trees[3];

    console.log(trees.length);

    //output 5

    使用delete删除数组元素,数组的长度不受影响

  2. output?

    var bar = true;
    console.log(bar + 0); //1
    console.log(bar + “xyz”); //1xyz
    console.log(bar + true); //2
    console.log(bar + false); //1

  3. output?

    var z = 1, y = z = typeof y;
    console.log(y);
    //undefined

    顺序从右向左

  4. output?

    // NFE (Named Function Expression
    var foo = function bar(){ return 12; };
    typeof bar();
    // not defined

  5. 函数声明和函数表达式的区别?

    用函数声明定义的函数,函数可以在函数声明之前调用,而用函数表达式定义的函数只能在声明之后调用。根本原因在于解析器对这两种定义方式读取的顺序不同:解析器会事先读取函数声明,即函数声明放在任意位置都可以被调用;对于函数表达式,解析器只有在读到函数表达式所在那行的时候才执行

  6. 什么是变量提升?

  7. output ?

var salary = "1000$";  

(function () {  
    console.log("Original salary was " + salary);  

    var salary = "5000$";  

    console.log("My New Salary " + salary);  
})();  
// undefined, 5000$ 变量提升
  1. instanceof 是什么?

    function foo(){

    return foo; 
    

    }
    new foo() instanceof foo;

    //false (return foo)

    instanceof可以检查对象是否是某个类型的对象

    类型检测的方法:

    • typeof
    • instanceof
    • object.prototype.toString
    • constructor
  2. counterArray的length?

    var counterArray = {

    A : 3,  
    B : 4  
    

    };
    counterArray[“C”] = 1;

Object.keys().length
也可以自己用for in 写一个方法
也可以用[Underscore工具库](http://underscorejs.org/)