JavaScript专业八级测试

  1. javascript ["1", "2", "3"].map(parseInt)
    解析:
    1
    2
    3
    ["1","2","3"].map(parseInt("1",0)) //1
    ["1","2","3"].map(parseInt("2",1)) //NaN
    ["1","2","3"].map(parseInt("3",2)) //NaN
    答案为:[1, NaN, NaN]
  2. javascript [typeof null, null instanceof Object]
    This is actually a long-standing bug in JavaScript, but it has not been fixed. null is not an object. But the result of typeof null is “object”.
    null is not an instance of Object
    答案为:['object' false]
  3. javascript [ [3,2,1].reduce(Math.pow), [].reduce(Math.pow)]
    解析:
    1
    2
    3
    [3,2,1].reduce((acc,cur)=>Match.pow(3,2)) //9
    [3,2,1].reduce((acc,cur)=>Match.pow(9,1))//9
    [].reduce(Math.pow)//报错,由于 reduce 没有初始值
    答案为:error
  4. javascript var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
    解析:
    由于 + 的运算符优先级高于三元运算符,所以会先执行'Value is ' + (val === 'smtg')// Value is true后执行'Value is true'?'Something' : 'Nothing' 所以最终结果为:’Something’
    答案为:Something
  5. code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    var name = 'World!';
    (function () {
    if (typeof name === 'undefined') {
    var name = 'Jack';
    console.log('Goodbye ' + name);
    } else {
    console.log('Hello ' + name);
    }
    })();
    解析:
    以上自执行函数形成了一个闭包,闭包会形成一个独立的作用域环境,val 声明的变量存在变量提升,所以typeof name === 'undefined'返回结果为 true 最终输出结果为:'Goodbye Jack'
    答案为:’Goodbye Jack’
  6. code:
    1
    2
    3
    4
    5
    6
    7
    var END = Math.pow(2, 53);
    var START = END - 100;
    var count = 0;
    for (var i = START; i <= END; i++) {
    count++;
    }
    console.log(count);
    解析:
    变量 STARTEND和差值为 100,也就是 for 循环会循环 100次,最终结果为: 100
    答案为:100
  7. code:
    1
    2
    3
    var ary = [0,1,2];
    ary[10] = 10;
    ary.filter(function(x) { return x === undefined;});
    答案为:[]
  8. code:
    1
    2
    3
    4
    5
    var two   = 0.2;
    var one = 0.1;
    var eight = 0.8;
    var six = 0.6;
    [two - one == one, eight - six == two]
    解析:
    由于浮点数的精度问题,所以two - one == one返回结果为 trueeight - six == two 0.8 - 0.6 equals 0.20000000000000007 返回结果为 false
    答案为:[true false]
  9. code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    function showCase(value) {
    switch(value) {
    case 'A':
    console.log('Case A');
    break;
    case 'B':
    console.log('Case B');
    break;
    case undefined:
    console.log('undefined');
    break;
    default:
    console.log('Do not know!');
    }
    }
    showCase(new String('A'));
    解析:
    由于 new String('A') 返回的是一个对象 String{‘A’},所以 switch 语句会返回 Do not know!
    答案为:’Do not know!’
  10. code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
      function showCase2(value) {
    switch(value) {
    case 'A':
    console.log('Case A');
    break;
    case 'B':
    console.log('Case B');
    break;
    case undefined:
    console.log('undefined');
    break;
    default:
    console.log('Do not know!');
    }
    }
    showCase2(String('A'));
    解析:
    由于 String('A') 返回的是一个字符串 ‘A’,所以 switch 语句会返回 Case A
    答案为:’Case A’
  11. code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    function isOdd(num) {
    return num % 2 == 1;
    }
    function isEven(num) {
    return num % 2 == 0;
    }
    function isSane(num) {
    return isEven(num) || isOdd(num);
    }
    var values = [7, 4, '13', -9, Infinity];
    values.map(isSane);
    解析:
    由于
    1
    2
    3
    4
    5
    7%2==0 || 7%2 == 1 //true
    4%2==0 || 4%2 == 1 //true
    '13'%2==0 || '13'%2 == 1 //true
    -9%2==0 || -9%2 == 1 //false
    Infinity%2==0 || Infinity%2 == 1 //false
    答案为:[true, true, true, false, false]
  12. code
    1
    2
    3
    parseInt(3, 8)
    parseInt(3, 2)
    parseInt(3, 0)
    parseInt(3, 8) -> 3, 3 是一个有效的八进制数,结果为:3
    parseInt(3, 2) -> NaN, 3 不是一个有效的二进制数,结果为:NaN
    parseInt(3, 0) -> 3, 0 代表十进制,结果为:3
    答案为: 3 NaN 3
  13. code:
    1
    Array.isArray( Array.prototype )
    答案为:true
  14. code:
    1
    2
    3
    4
    5
    6
    var a = [0];
    if ([0]) {
    console.log(a == true);
    } else {
    console.log("wut");
    }
    答题解析:
    在 if() 语句中,所有值都会被转换成 boolean 类型,所以 [0] 会被转换成 true,所以会进入console.log(a == true);
    当比较数组和布尔值时,两边都会被转换为数字,[0].toString() -> ‘0’转换为数字 -> 0, 0 == true -> false, 所以最终结果为:false
    答案为:false
  15. code:
    1
    []==[]
    答案为:false
  16. code:
    1
    2
     '5' + 3  
    '5' - 3
    答案为:'53' 2
  17. code:
    1
    1 + - + + + - + 1 
    解析:
    1+(- + + + - + 1) -> 1 + - + + + - (+ 1) -> 1 + - + + + (- + 1)->1 + - (+ + + - + 1)->1 + - -1 -> 1 + (- - 1)->1 + 1 -> 2
    答案为:2
  18. code:
    1
    2
    3
     var ary = Array(3);
    ary[0]=2
    ary.map(function(elem) { return '1'; });
    数组长度为 3,但是只有一个元素,所以最终结果为:['1',,]
    答案为:[‘1’,,]
  19. code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
     function sidEffecting(ary) { 
    ary[0] = ary[2];
    }
    function bar(a,b,c) {
    c = 10
    sidEffecting(arguments);
    return a + b + c;
    }
    bar(1,1,1)
    解析:
    在非严格模式下sidEffecting(arguments) 会改变 arguments 的值,所以最终结果为:21
    答案为:21
  20. code:
    1
    2
    3
      var a = 111111111111111110000,
    b = 1111;
    a + b;
    因为 a 是一个 超过 2^53 的大整数,导致 a 的精度丢失,所以最终结果为:111111111111111110000
    答案为:111111111111111110000
  21. code:
    1
    Number.MIN_VALUE > 0
    Number.MIN_VALUE 是 JavaScript 中最小的正数,所以最终结果为:true
    答案为:true
  22. code:
    1
    [1 < 2 < 3, 3 < 2 < 1]
    解析:
    由于 < 1 < 2 < 3->true < 3 -> true, 3 < 2 < 1 -> false < 1 -> true 所以结果为:[true true]
    答案为:[true true]
  23. code:
    1
    2 == [[[2]]]
    答题解析:
    [[[2]]].toString()->’2’ 2 == ‘2’ -> true 所以最终结果为:true
    答案为:true
  24. code:
    1
    2
    3
    3.toString()
    3..toString()
    3...toString()
    解析:
    点运算符会被优先识别为数字常量的一部分,然后才是对象属性访问符:
    3.toString()会被解析为:(3.)toString(),所以会报错
    3..toString()会被解析为(3.).toString(),结果为:’3’
    3...toString()会被解析为(3.)..toString(),会报错
    答案为:’报错,’3’,报错’
  25. code:
    1
    2
    3
    4
    5
     (function(){
    var x = y = 1;
    })();
    console.log(y);
    console.log(x);
    解析:
    由于 var x = y = 1 会被解析为 y = 1; var x = y; 所以 y 会被定义为全局变量,x 会被定义为局部变量,所以最终结果为:1 undefined
    答案为:’1 undefined’
  26. code:
    1
    2
    3
     var a = /123/, b = /123/;
    a == b
    a === b
    解析:
    由于正则表达式是对象,所以 a == ba === b 会比较两个对象的引用,所以最终结果为:false false
    答案为:false false
  27. code:
    1
    2
    3
    4
    5
    6
    7
     var a = [1, 2, 3],
    b = [1, 2, 3],
    c = [1, 2, 4]
    a == b
    a === b
    a > c
    a < c
    解析:
    由于数组是对象,所以 a == ba === b 会比较两个对象的引用,所以a == b 和 a === b 结果为:false false
    由于数组会被转换为字符串,所以 a > ca < c 会比较两个数组的字符串形式,a -> ‘1,2,3’ c -> ‘1,2,4’,两个字符串比较如果首字母相等,会继续比较下一个字符,直到比较完成 所以最终结果为:false true
    答案为:false true
  28. code:
    1
    2
     var a = {}, b = Object.prototype;
    [a.prototype === b, Object.getPrototypeOf(a) === b]
    解析:
    由于对象没有 prototype 属性,所以 a.prototype 会返回 undefined,所以最终结果为:[false true]
    答案为:[false true]
  29. code:
    1
    2
    3
     function f() {}
    var a = f.prototype, b = Object.getPrototypeOf(f);
    a === b
    解析:
    由于函数的 prototype 属性是一个对象,而Object.getPrototypeOf(f) 是函数,所以最终结果为:false
    答案为:false
  30. code:
    1
    2
    3
    4
    function foo() { }
    var oldName = foo.name;
    foo.name = "bar";
    [oldName, foo.name]
    解析:
    The result shows ["foo", "foo"]. This demonstrates that:
    The initial foo.name is “foo” (which gets stored in oldName)
    Even though we try to change foo.name to “bar”, it remains “foo”
    This is because the name property of functions in JavaScript is read-only by default (it’s a non-writable property). In strict mode, attempting to change it would throw an error. In non-strict mode, the assignment is silently ignored.
    Object.getOwnPropertyDescriptor(foo, 'name').writable // false
    答案为:["foo", "foo"]
  31. code:
    1
    "1 2 3".replace(/\d/g, parseInt)
    解析:
    First digit: parseInt(“1”, 0) → 1 (radix 0 is treated as 10)
    Second digit: parseInt(“2”, 2) → NaN (invalid binary digit)
    Third digit: parseInt(“3”, 4) → 3 (valid in base 4)
    So the result is: “1 NaN 3”
    答案为:"1 NaN 3"
  32. code:
    1
    2
    3
    4
    5
    6
     function f() {}
    var parent = Object.getPrototypeOf(f);
    f.name // ?
    parent.name // ?
    typeof eval(f.name) // ?
    typeof eval(parent.name) // ?
    解析:
    f.name -> “f”
    parent.name -> ‘’
    typeof eval(f.name) -> “function”
    typeof eval(parent.name) -> “undefined”
  33. code:
    1
    2
    var lowerCaseOnly =  /^[a-z]+$/;
    [lowerCaseOnly.test(null), lowerCaseOnly.test()]
    解析:
    The result is [true, true]. This demonstrates that:test在检测时会隐性将内容转为字符串,其实等同于:’[lowerCaseOnly.test(‘null’), lowerCaseOnly.test(‘undefined’)]’
    答案为:[true, true]
  34. code:
    1
    [,,,].join(", ")
    解析:
    The result is ", , ". This demonstrates that:
    Empty array slots are treated as undefined
    When .join() is called with a separator, it places the separator between each element
    In this case, there are three slots, so two separators are inserted
    So the output is literally: , ,
    答案为:, ,
  35. code:
    1
    2
    var a = {class: "Animal", name: 'Fido'};
    a.class
    解析:
    The result is "Animal". This demonstrates that:This creates an object a with two properties:
    class with the value “Animal”
    name with the value “Fido” 答案为:“Animal”`