zoukankan      html  css  js  c++  java
  • 前端经典面试题 不经典不要star!

    前言

    (以下内容为一个朋友所述)今天我想跟大家分享几个前端经典的面试题,为什么我突然想写这么一篇文章呢?今天我应公司要求去面试了下几位招聘者,然后又现场整不出几个难题,就搜了一下前端变态面试题! HAHA,前提我并不是一个变态,欺负人的面试官.只是我希望看看对方的逻辑能力!

    从而又拿这些面试题进行了自我检测,发现还是有一些坑的~~~
    接下与大家进行几道题的分析 分享 互勉!

    正文

    先把我挑选的几道,不一定最经典.但是会让你有学习的进步!列举一下

    //第1题
     ["1", "2", "3"].map(parseInt)
     
    //第2题
    [ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]
    
    //第3题
    var ary = [0,1,2];
    ary[10] = 10;
    ary.filter(function(x) { return x === undefined;});
    
    //第4题
    [typeof null, null instanceof Object]
    
    //第5题
    function sidEffecting(ary) {
      ary[0] = ary[2];
     }
    function bar(a,b,c) {
       c = 10
       sidEffecting(arguments);
       return a + b + c;
    }
    bar(1,1,1)
    
    //第六题
     var END = Math.pow(2, 53);
     var START = END - 100;
     var count = 0;
     for (var i = START; i <= END; i++) {
         count++;
     }
     console.log(count);
     
    

    读者思考时间

    大家努力思考,努力!==============================================

    接下来一道一道咱们去慢慢解析

    第一题:

    ["1", "2", "3"].map(parseInt)
    

    这道题知识点包括:

    1. Array/map
    2. Number/parseInt
    3. JavaScript parseInt

    按照上面知识点来串一下这道题!

    首先, map接受两个参数, 一个回调函数 callback, 一个回调函数的this值
    
    其中回调函数接受三个参数 currentValue, index, arrary;
    
    而题目中, map只传入了回调函数--parseInt.
    
    其次, parseInt 只接受两个两个参数 string, radix(基数).  
    本题理解来说也就是key与 index 
    
    所以本题即问
    
    parseInt('1', 0);
    parseInt('2', 1);
    parseInt('3', 2);
    首先后两者参数不合法.
    
    所以答案是 [1, NaN, NaN]
    
      如果研究理解了 
      parseInt(3, 8)
      parseInt(3, 2)    //下方评论该题答案  别作弊哦!
      parseInt(3, 0)
      
    

    第二题:

    [ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]

    这道题知识点:

    • Array/Reduce

    穿插知识点来一次这道题!

    arr.reduce(callback[, initialValue])
    
    reduce接受两个参数, 一个回调, 一个初始值.
    
    回调函数接受四个参数 previousValue, currentValue, currentIndex, array
    
    需要注意的是 If the array is empty and no initialValue was provided, TypeError would be thrown.
    
    所以第二个表达式会报异常. 第一个表达式等价于 Math.pow(3, 2) => 9; Math.pow(9, 1) =>9
    

    答案 an error

    第三题:

    var ary = [0,1,2];
    ary[10] = 10;
    ary.filter(function(x) { return x === undefined;});
    
    答案是 []

    知识点是:

    • 稀疏数组

    我们来看一下 Array.prototype.filter 的 polyfill:

    if (!Array.prototype.filter) {
      Array.prototype.filter = function(fun/*, thisArg*/) {
        'use strict';
    
        if (this === void 0 || this === null) {
          throw new TypeError();
        }
    
        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun !== 'function') {
          throw new TypeError();
        }
    
        var res = [];
        var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
        for (var i = 0; i < len; i++) {
          if (i in t) { // 注意这里!!!
            var val = t[i];
            if (fun.call(thisArg, val, i, t)) {
              res.push(val);
            }
          }
        }
    
        return res;
      };
    }

    我们看到在迭代这个数组的时候, 首先检查了这个索引值是不是数组的一个属性, 那么我们测试一下.

    0 in ary; => true
    3 in ary; => false
    10 in ary; => true

    也就是说 从 3 - 9 都是没有初始化的bug !, 这些索引并不存在与数组中. 在 array 的函数调用的时候是会跳过这些坑的.

    第四题:

    [typeof null, null instanceof Object]

    知识点:

    1. Operators/typeof
    2. Operators/instanceof

    typeof 返回一个表示类型的字符串.

    instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上.

    这个题可以直接看链接... 因为 typeof null === 'object' 自语言之初就是这样....

    typeof 的结果请看下表:

    type         result
    Undefined   "undefined"
    Null        "object"
    Boolean     "boolean"
    Number      "number"
    String      "string"
    Symbol      "symbol"
    Host object Implementation-dependent
    Function    "function"
    Object      "object"
    

    所以答案 [object, false]

    第五题:

    function sidEffecting(ary) {
      ary[0] = ary[2];
     }
    function bar(a,b,c) {
       c = 10
       sidEffecting(arguments);
       return a + b + c;
    }
    bar(1,1,1)
    

    知识点:

    • Functions/arguments

    首先 The arguments object is an Array-like object corresponding to the arguments passed to a function.

    也就是说 arguments 是一个 object, c 就是 arguments[2], 所以对于 c 的修改就是对 arguments[2] 的修改.

    所以答案是 21.

    但是!!!!

    当函数参数涉及到 any rest parameters, any default parameters or any destructured parameters 的时候, 这个 arguments 就不在是一个 mapped arguments object 了.....

    请看:

    function sidEffecting(ary) {
      ary[0] = ary[2];
    }
    function bar(a,b,c=3) {
      c = 10
      sidEffecting(arguments);
      return a + b + c;
    }
    bar(1,1,1)

    答案是 12 !!!!

    请慢慢体会!!

    第六题:

    咳咳咳!
    细心的小伙伴发现了我第6题不是第6题而是第
    其实这个是给你们留下一个思考的题 如果有疑问或者探讨请留言!

    【我有一个前端学习交流QQ群:328058344 如果你在学习前端的过程中遇到什么问题,欢迎来我的QQ群提问,群里每天还会更新一些学习资源。禁止闲聊,非喜勿进。】

  • 相关阅读:
    用了7年做到项目经理,女朋友却离开了我
    手把手教你建网站--程序小白适用篇
    燃尽图的学习与理解
    每周进度
    四人组队
    读构建之法之感
    两人组队,小学生的四则运算
    词汇统计
    AMQP中的架构和组成元素
    MSSQL机制
  • 原文地址:https://www.cnblogs.com/qianduantuanzhang/p/8184257.html
Copyright © 2011-2022 走看看