De-yu's Note
  • Deyu Notebook
  • Side Project
    • Stock
    • ChatBox
    • SnowCraft
    • ScrollBar
  • 架構問題
    • 解決 RTK Query data 為 undefined 的實務做法
    • 透過 xstate 解決UI 狀態問題
  • 技術觀點
    • 為什麼需要 store
    • Router 作用
    • react 和 next.js 差異
    • monoRepo vs Multiple Repo
  • Performance
    • React 優化
  • JS Coding
    • Curry
    • Debounce
    • Throttle
  • map
  • memo()
  • Promise 實作
  • Promise Function
  • Testing
    • 使用 Jest 、 React Testing Library 、MSW 建立測試環境
  • Miscellaneous
    • Event Loop
    • Browser
    • Code Review
    • Storage
  • AMD 、 CommonJS 、 ES modules
  • JWT
  • Next.js
  • 用過的 module
  • Internet
    • UDP
  • TCP/IP
  • SSL TLS
  • HTTP
  • AI 工作流
    • Page 1
Powered by GitBook
On this page

map

實作 Array.map

在這邊遇到幾個有趣的問題
for..in 會將可列舉屬性列出 包含 myMap 這個 protoType 的方法

new Array 宣告完 Array 後
Array 的值雖然是 undefiend 但
如果 console 會出現叫 empty item 的東西 
是無法被列舉出來的屬性 

Array.prototype.myMap = function(func, thisObj) {
  const newArr = new Array(this.length);
  for(const index in this) {
    if(index === 'myMap') {
      continue;
    }
    newArr[index] = (func.call(thisObj, this[index], Number(index), this))
  }
  return newArr
}
PreviousThrottleNextmemo()

Last updated 28 days ago