前端经典面试题 不经典不要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)這道題知識點包括:
按照上面知識點來串一下這道題!
首先, 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*/) {我們看到在迭代這個數組的時候, 首先檢查了這個索引值是不是數組的一個屬性, 那么我們測試一下.
0 in ary; => true 3 in ary; => false 10 in ary; => true也就是說 從 3 - 9 都是沒有初始化的
bug?!, 這些索引并不存在與數組中. 在 array 的函數調用的時候是會跳過這些坑的.第四題:
[typeof?null,?null?instanceof?Object]知識點:
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 !!!!
請慢慢體會!!
第六題:
咳咳咳!
細心的小伙伴發現了我第
其實這個是給你們留下一個思考的題 如果有疑問或者探討請留言!
【我有一個前端學習交流QQ群:328058344 如果你在學習前端的過程中遇到什么問題,歡迎來我的QQ群提問,群里每天還會更新一些學習資源。禁止閑聊,非喜勿進。】
總結
以上是生活随笔為你收集整理的前端经典面试题 不经典不要star!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从如何停掉 Promise 链说起
- 下一篇: Web前端开发学习误区,你掉进去了没?