成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

32個手撕JS,徹底擺脫初級前端(面試高頻)-上篇

開發
作為前端開發,JS是重中之重,最近結束了面試的高峰期,基本上offer也定下來了就等開獎,趁著這個時間總結下32個手撕JS問題,這些都是高頻面試題,完全理解之后定能徹底擺脫初級前端。

[[344056]]

 關于源碼都緊遵規范,都可跑通MDN示例,其余的大多會涉及一些關于JS的應用題和本人面試過程

01.數組扁平化
數組扁平化是指將一個多維數組變為一個一維數組

  1. const arr = [1, [2, [3, [4, 5]]], 6]; 
  2. // => [1, 2, 3, 4, 5, 6] 
  3. 復制代碼 

方法一:使用flat()

  1. const res1 = arr.flat(Infinity); 
  2. 復制代碼 

方法二:利用正則

  1. const res2 = JSON.stringify(arr).replace(/\[|\]/g, '').split(','); 
  2. 復制代碼 

但數據類型都會變為字符串

方法三:正則改良版本

  1. const res3 = JSON.parse('[' + JSON.stringify(arr).replace(/\[|\]/g, '') + ']'); 
  2. 復制代碼 

方法四:使用reduce

  1. const flatten = arr => { 
  2.   return arr.reduce((pre, cur) => { 
  3.     return pre.concat(Array.isArray(cur) ? flatten(cur) : cur); 
  4.   }, []) 
  5. const res4 = flatten(arr); 
  6. 復制代碼 

方法五:函數遞歸

  1. const res5 = []; 
  2. const fn = arr => { 
  3.   for (let i = 0; i < arr.length; i++) { 
  4.     if (Array.isArray(arr[i])) { 
  5.       fn(arr[i]); 
  6.     } else { 
  7.       res5.push(arr[i]); 
  8.     } 
  9.   } 
  10. fn(arr); 
  11. 復制代碼 

02.數組去重

  1. const arr = [1, 1, '1', 17, truetruefalsefalse'true''a', {}, {}]; 
  2. // => [1, '1', 17, truefalse'true''a', {}, {}] 
  3. 復制代碼 

方法一:利用Set

  1. const res1 = Array.from(new Set(arr)); 
  2. 復制代碼 

方法二:兩層for循環+splice

  1. const unique1 = arr => { 
  2.   let len = arr.length; 
  3.   for (let i = 0; i < len; i++) { 
  4.     for (let j = i + 1; j < len; j++) { 
  5.       if (arr[i] === arr[j]) { 
  6.         arr.splice(j, 1); 
  7.         // 每刪除一個樹,j--保證j的值經過自加后不變。同時,len--,減少循環次數提升性能 
  8.         len--; 
  9.         j--; 
  10.       } 
  11.     } 
  12.   } 
  13.   return arr; 
  14. 復制代碼 

方法三:利用indexOf

  1. const unique2 = arr => { 
  2.   const res = []; 
  3.   for (let i = 0; i < arr.length; i++) { 
  4.     if (res.indexOf(arr[i]) === -1) res.push(arr[i]); 
  5.   } 
  6.   return res; 
  7. 復制代碼 

當然也可以用include、filter,思路大同小異。

方法四:利用include

  1. const unique3 = arr => { 
  2.   const res = []; 
  3.   for (let i = 0; i < arr.length; i++) { 
  4.     if (!res.includes(arr[i])) res.push(arr[i]); 
  5.   } 
  6.   return res; 
  7. 復制代碼 

方法五:利用filter

  1. const unique4 = arr => { 
  2.   return arr.filter((item, index) => { 
  3.     return arr.indexOf(item) === index
  4.   }); 
  5. 復制代碼 

方法六:利用Map

  1. const unique5 = arr => { 
  2.   const map = new Map(); 
  3.   const res = []; 
  4.   for (let i = 0; i < arr.length; i++) { 
  5.     if (!map.has(arr[i])) { 
  6.       map.set(arr[i], true
  7.       res.push(arr[i]); 
  8.     } 
  9.   } 
  10.   return res; 
  11. 復制代碼 

03.類數組轉化為數組
類數組是具有length屬性,但不具有數組原型上的方法。常見的類數組有arguments、DOM操作方法返回的結果。

方法一:Array.from

  1. Array.from(document.querySelectorAll('div')) 
  2. 復制代碼 

方法二:Array.prototype.slice.call()

  1. Array.prototype.slice.call(document.querySelectorAll('div')) 
  2. 復制代碼 

方法三:擴展運算符

  1. [...document.querySelectorAll('div')] 
  2. 復制代碼 

方法四:利用concat

  1. Array.prototype.concat.apply([], document.querySelectorAll('div')); 
  2. 復制代碼 

04.Array.prototype.filter()

  1. rray.prototype.filter = function(callback, thisArg) { 
  2.   if (this == undefined) { 
  3.     throw new TypeError('this is null or not undefined'); 
  4.   } 
  5.   if (typeof callback !== 'function') { 
  6.     throw new TypeError(callback + 'is not a function'); 
  7.   } 
  8.   const res = []; 
  9.   // 讓O成為回調函數的對象傳遞(強制轉換對象) 
  10.   const O = Object(this); 
  11.   // >>>0 保證len為number,且為正整數 
  12.   const len = O.length >>> 0; 
  13.   for (let i = 0; i < len; i++) { 
  14.     // 檢查i是否在O的屬性(會檢查原型鏈) 
  15.     if (i in O) { 
  16.       // 回調函數調用傳參 
  17.       if (callback.call(thisArg, O[i], i, O)) { 
  18.         res.push(O[i]); 
  19.       } 
  20.     } 
  21.   } 
  22.   return res; 
  23. 復制代碼 

對于>>>0有疑問的:解釋>>>0的作用

05.Array.prototype.map()

  1. Array.prototype.map = function(callback, thisArg) { 
  2.   if (this == undefined) { 
  3.     throw new TypeError('this is null or not defined'); 
  4.   } 
  5.   if (typeof callback !== 'function') { 
  6.     throw new TypeError(callback + ' is not a function'); 
  7.   } 
  8.   const res = []; 
  9.   // 同理 
  10.   const O = Object(this); 
  11.   const len = O.length >>> 0; 
  12.   for (let i = 0; i < len; i++) { 
  13.     if (i in O) { 
  14.       // 調用回調函數并傳入新數組 
  15.       res[i] = callback.call(thisArg, O[i], i, this); 
  16.     } 
  17.   } 
  18.   return res; 
  19. 復制代碼 

06.Array.prototype.forEach()

forEach跟map類似,唯一不同的是forEach是沒有返回值的。

  1. Array.prototype.forEach = function(callback, thisArg) { 
  2.   if (this == null) { 
  3.     throw new TypeError('this is null or not defined'); 
  4.   } 
  5.   if (typeof callback !== "function") { 
  6.     throw new TypeError(callback + ' is not a function'); 
  7.   } 
  8.   const O = Object(this); 
  9.   const len = O.length >>> 0; 
  10.   let k = 0; 
  11.   while (k < len) { 
  12.     if (k in O) { 
  13.       callback.call(thisArg, O[k], k, O); 
  14.     } 
  15.     k++; 
  16.   } 
  17. 復制代碼 

07.Array.prototype.reduce()

  1. Array.prototype.reduce = function(callback, initialValue) { 
  2.   if (this == undefined) { 
  3.     throw new TypeError('this is null or not defined'); 
  4.   } 
  5.   if (typeof callback !== 'function') { 
  6.     throw new TypeError(callbackfn + ' is not a function'); 
  7.   } 
  8.   const O = Object(this); 
  9.   const len = this.length >>> 0; 
  10.   let accumulator = initialValue; 
  11.   let k = 0; 
  12.   // 如果第二個參數為undefined的情況下 
  13.   // 則數組的第一個有效值作為累加器的初始值 
  14.   if (accumulator === undefined) { 
  15.     while (k < len && !(k in O)) { 
  16.       k++; 
  17.     } 
  18.     // 如果超出數組界限還沒有找到累加器的初始值,則TypeError 
  19.     if (k >= len) { 
  20.       throw new TypeError('Reduce of empty array with no initial value'); 
  21.     } 
  22.     accumulator = O[k++]; 
  23.   } 
  24.   while (k < len) { 
  25.     if (k in O) { 
  26.       accumulator = callback.call(undefined, accumulator, O[k], k, O); 
  27.     } 
  28.     k++; 
  29.   } 
  30.   return accumulator; 
  31. 復制代碼 

08.Function.prototype.apply()
第一個參數是綁定的this,默認為window,第二個參數是數組或類數組

  1. Function.prototype.apply = function(context = window, args) { 
  2.   if (typeof this !== 'function') { 
  3.     throw new TypeError('Type Error'); 
  4.   } 
  5.   const fn = Symbol('fn'); 
  6.   context[fn] = this; 
  7.  
  8.   const res = context[fn](...args); 
  9.   delete context[fn]; 
  10.   return res; 
  11. 復制代碼 

09.Function.prototype.call
于call唯一不同的是,call()方法接受的是一個參數列表

  1. Function.prototype.call = function(context = window, ...args) { 
  2.   if (typeof this !== 'function') { 
  3.     throw new TypeError('Type Error'); 
  4.   } 
  5.   const fn = Symbol('fn'); 
  6.   context[fn] = this; 
  7.  
  8.   const res = this[fn](...args); 
  9.   delete this.fn; 
  10.   return res; 
  11. 復制代碼 

10.Function.prototype.bind

  1. Function.prototype.bind = function(context, ...args) { 
  2.   if (typeof this !== 'function') { 
  3.     throw new Error("Type Error"); 
  4.   } 
  5.   // 保存this的值 
  6.   var self = this; 
  7.  
  8.   return function F() { 
  9.     // 考慮new的情況 
  10.     if(this instanceof F) { 
  11.       return new self(...args, ...arguments) 
  12.     } 
  13.     return self.apply(context, [...args, ...arguments]) 
  14.   } 
  15. 復制代碼 

11.debounce(防抖)
觸發高頻時間后n秒內函數只會執行一次,如果n秒內高頻時間再次觸發,則重新計算時間。

  1. const debounce = (fn, time) => { 
  2.   let timeout = null
  3.   return function() { 
  4.     clearTimeout(timeout) 
  5.     timeout = setTimeout(() => { 
  6.       fn.apply(this, arguments); 
  7.     }, time); 
  8.   } 
  9. }; 
  10. 復制代碼 

防抖常應用于用戶進行搜索輸入節約請求資源,window觸發resize事件時進行防抖只觸發一次。

12.throttle(節流)
高頻時間觸發,但n秒內只會執行一次,所以節流會稀釋函數的執行頻率。

  1. const throttle = (fn, time) => { 
  2.   let flag = true
  3.   return function() { 
  4.     if (!flag) return
  5.     flag = false
  6.     setTimeout(() => { 
  7.       fn.apply(this, arguments); 
  8.       flag = true
  9.     }, time); 
  10.   } 
  11. 復制代碼 

節流常應用于鼠標不斷點擊觸發、監聽滾動事件。

13.函數珂里化

  1. 指的是將一個接受多個參數的函數 變為 接受一個參數返回一個函數的固定形式,這樣便于再次調用,例如f(1)(2) 

經典面試題:實現add(1)(2)(3)(4)=10; 、 add(1)(1,2,3)(2)=9;

  1. function add() { 
  2.   const _args = [...arguments]; 
  3.   function fn() { 
  4.     _args.push(...arguments); 
  5.     return fn; 
  6.   } 
  7.   fn.toString = function() { 
  8.     return _args.reduce((sum, cur) => sum + cur); 
  9.   } 
  10.   return fn; 
  11. 復制代碼 

14.模擬new操作
3個步驟:

  1. 以ctor.prototype為原型創建一個對象。
  2. 執行構造函數并將this綁定到新創建的對象上。
  3. 判斷構造函數執行返回的結果是否是引用數據類型,若是則返回構造函數執行的結果,否則返回創建的對象。
  1. function newOperator(ctor, ...args) { 
  2.   if (typeof ctor !== 'function') { 
  3.     throw new TypeError('Type Error'); 
  4.   } 
  5.   const obj = Object.create(ctor.prototype); 
  6.   const res = ctor.apply(obj, args); 
  7.  
  8.   const isObject = typeof res === 'object' && res !== null
  9.   const isFunction = typeof res === 'function'
  10.   return isObject || isFunction ? res : obj; 
  11. 復制代碼 

15.instanceof
instanceof運算符用于檢測構造函數的prototype屬性是否出現在某個實例對象的原型鏈上。

  1. const myInstanceof = (leftright) => { 
  2.   // 基本數據類型都返回false 
  3.   if (typeof left !== 'object' || left === nullreturn false
  4.   let proto = Object.getPrototypeOf(left); 
  5.   while (true) { 
  6.     if (proto === nullreturn false
  7.     if (proto === right.prototype) return true
  8.     proto = Object.getPrototypeOf(proto); 
  9.   } 
  10. 復制代碼 

16.原型繼承
這里只寫寄生組合繼承了,中間還有幾個演變過來的繼承但都有一些缺陷

  1. function Parent() { 
  2.   this.name = 'parent'
  3. function Child() { 
  4.   Parent.call(this); 
  5.   this.type = 'children'
  6. Child.prototype = Object.create(Parent.prototype); 
  7. Child.prototype.constructor = Child; 
  8. 復制代碼 

 

 

責任編輯:姜華 來源: 前端UpUp
相關推薦

2021-06-09 07:01:30

前端CallApply

2021-09-06 08:13:35

APM系統監控

2021-07-15 14:29:06

LRU算法

2021-05-18 07:52:31

PromiseAsyncAwait

2024-08-06 10:16:52

Java AgentJava

2024-12-03 16:49:58

2020-09-15 08:55:07

算法數據基礎

2020-09-17 14:04:32

拷貝

2023-06-25 08:38:09

多線程循環打印

2023-08-02 08:54:58

Java弱引用鏈表

2020-09-16 14:17:42

flat方法

2021-10-31 07:38:37

排序算法代碼

2023-09-18 09:10:11

Golang高性能緩存庫

2015-08-21 10:38:16

編程語言GoC語言

2025-04-01 08:25:00

OSPF網絡IT

2019-11-26 10:30:11

CSS前端面試題

2025-04-03 09:56:40

Python算法開發

2022-04-15 09:23:29

Kubernetes面試題

2021-02-23 12:43:39

Redis面試題緩存
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产一级一级 | 久久亚洲精品国产精品紫薇 | 成人深夜福利在线观看 | 中文字幕日韩一区 | www在线| 国产精品久久久久久久久久 | 久久新| 欧美日韩午夜精品 | 国产精品地址 | 手机日韩| 久久精品欧美一区二区三区不卡 | 欧美一区二区在线观看 | 日本精品久久久久久久 | 欧美一区精品 | 亚洲另类春色偷拍在线观看 | 久久精品亚洲精品国产欧美 | 一区二区三区国产精品 | 久久av网站| 夜夜骑首页 | 成人精品国产 | 古装人性做爰av网站 | 夜夜操操操 | 国产精品免费观看视频 | 免费观看av网站 | 中文字字幕在线中文乱码范文 | 一区在线观看 | 亚洲精品一区二区久 | 美女精品一区 | 久久av一区| 日韩精品在线观看免费 | 中文字幕av色 | 成人av一区 | 日本又色又爽又黄的大片 | 成人av久久 | 91久久精品 | 日韩综合一区 | 中文字幕在线观看日韩 | 888久久久 | 久久人体视频 | 欧美日韩国产精品一区 | 天堂在线91 |