亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

常用前端手寫功能進(jìn)階示例詳解

 更新時間:2022年07月05日 11:52:40   作者:前端阿飛  
這篇文章主要為大家介紹了前端手寫功能進(jìn)階的相關(guān)技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1、Promise.all

Promise.myAll = function (promises) {
  return new Promise((resolve, reject) => {
    // promises 可以不是數(shù)組,但必須要具有 Iterator 接口
    if (typeof promises[Symbol.iterator] !== 'function') {
      reject('TypeError: promises is not iterable')
    }
    if (promises.length === 0) {
      resolve([])
    } else {
      const res = []
      const len = promises.length
      let count = 0
      for (let i = 0; i < len; i++) {
        // Promise.resolve 的作用是將普通值或 thenable 對象轉(zhuǎn)為 promise,promise 則直接返回
        Promise.resolve(promises[i])
          .then((data) => {
            res[i] = data
            count += 1
            if (count === len) {
              resolve(res)
            }
          })
          .catch((err) => {
            reject(err)
          })
      }
    }
  })
}
// test
function p1() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, 1)
  })
}
function p2() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, 2)
  })
}
Promise.myAll([p1(), p2()]).then(res => {
  console.log(res) // [1, 2]
})

2、Promise.race

Promise.myRace = function (promises) {
  return new Promise((resolve, reject) => {
    // promises 可以不是數(shù)組,但必須要具有 Iterator 接口
    if (typeof promises[Symbol.iterator] !== 'function') {
      reject('TypeError: promises is not iterable')
    }
    for (const item of promises) {
      // 先出來的結(jié)果會被 resolve 或 reject 出去, 一旦狀態(tài)變化就不會再變
      Promise.resolve(item).then(resolve, reject)
    }
  })
}
// test
function p1() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, 1)
  })
}
function p2() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, 2)
  })
}
Promise.myRace([p1(), p2()]).then((res) => {
  console.log(res) // 1
})

3、Promise.any

Promise.myAny = function (promises) {
  return new Promise((resolve, reject) => {
    // promises 可以不是數(shù)組,但必須要具有 Iterator 接口
    if (typeof promises[Symbol.iterator] !== 'function') {
      reject('TypeError: promises is not iterable')
    }
    const len = promises.length
    let count = 0
    for (let i = 0; i < len; i++) {
      Promise.resolve(promises[i]).then(resolve, (err) => {
        count += 1
        if (count === promises.length) {
          reject(new Error('所有 promise 都失敗'))
        }
      })
    }
  })
}
// test
function p1() {
  return new Promise((resolve, reject) => {
    setTimeout(reject, 1000, 1)
  })
}
function p2() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, 2)
  })
}
Promise.myAny([p1(), p2()]).then((res) => {
  console.log(res) // 2
})

4、冒泡排序

function bubbleSort(arr) {
  let len = arr.length
  for (let i = 0; i < len - 1; i++) {
    // 從第一個元素開始,比較相鄰的兩個元素,前者大就交換位置
    for (let j = 0; j < len - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
        // 交換位置
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
      }
    }
    // 每次遍歷結(jié)束,都能找到一個最大值,放在數(shù)組最后
  }
  return arr
}
// test
const arr = [3, 1, 2, 5, 4]
console.log(bubbleSort(arr)) // [1, 2, 3, 4, 5]

5、選擇排序

function selectSort(arr) {
  let len = arr.length
  for (let i = 0; i < len - 1; i++) {
    // 假設(shè)每次循環(huán),最小值就是第一個
    let minIndex = i
    for (let j = i + 1; j < len; j++) {
      // 如果最小值大于其他的值,則修改索引,從而找到真正的最小值
      if (arr[minIndex] > arr[j]) {
        minIndex = j
      }
    }
    // 最小值和第一個交換位置
    [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]
  }
  return arr
}
// test
const arr = [3, 1, 2, 5, 4]
console.log(bubbleSort(arr)) // [1, 2, 3, 4, 5]

6、快速排序

function quickSort(arr) {
  if (arr.length <= 1) return arr
  // 每次取第一個元素作為基準(zhǔn)值
  const pivot = arr.shift()
  const left = []
  const right = []
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < pivot) {
      // 如果小于基準(zhǔn)值,則把它放在左數(shù)組
      left.push(arr[i])
    } else {
      // 如果大于等于基準(zhǔn)值,則放在右數(shù)組
      right.push(arr[i])
    }
  }
  // 遞歸處理,并把左中右三個數(shù)組拼接起來
  return quickSort(left).concat([pivot], quickSort(right))
}
// test
const arr = [3, 1, 2, 5, 4]
console.log(quickSort(arr)) // [1, 2, 3, 4, 5]

7、call

Function.prototype.myCall = function (context = globalThis) {
  // 把方法添加到 context 上,這樣使用context[key]調(diào)用的時候,內(nèi)部的 this 就指向了 context
  // 使用 Symbol 防止 context 原有屬性被覆蓋
  const key = Symbol('key')
  context[key] = this
  const args = [...arguments].slice(1)
  const res = context[key](...args)
  delete context[key]
  return res
}
// test
const myName = { name: 'Jack' }
function say() {
  const [age, height] = arguments
  console.log(`My name is ${this.name}, My age is ${age}, My height is ${height}`)
}
say.myCall(myName, 16, 170) // My name is Jack, My age is 16, My height is 170

8、apply

Function.prototype.myApply = function (context = globalThis) {
  // 把方法添加到 context 上,這樣使用context[key]調(diào)用的時候,內(nèi)部的 this 就指向了 context
  // 使用 Symbol 防止 context 原有屬性被覆蓋
  const key = Symbol('key')
  context[key] = this
  let res
  if (arguments[1]) {
    res = context[key](...arguments[1])
  } else {
    res = context[key]()
  }
  delete context[key]
  return res
}
// test
const myName = { name: 'Jack' }
function say() {
  const [age, height] = arguments
  console.log(`My name is ${this.name}, My age is ${age}, My height is ${height}`)
}
say.myApply(myName, [16, 170]) // My name is Jack, My age is 16, My height is 170

9、bind

Function.prototype.myBind = function (context = globalThis) {
  const fn = this
  const args = [...arguments].slice(1)
  const newFunc = function () {
    const newArgs = args.concat(...arguments)
    if (this instanceof newFunc) {
      // 通過 new 調(diào)用,this 為新創(chuàng)建的對象實例,將函數(shù)內(nèi)部的 this 替換為這個新對象
      fn.apply(this, newArgs)
    } else {
      // 普通方式調(diào)用,將函數(shù)內(nèi)部的 this 替換為 context
      fn.apply(context, newArgs)
    }
  }
  // 支持 new 調(diào)用
  newFunc.prototype = Object.create(fn.prototype)
  return newFunc
}
// test
const myName = { name: 'Jack' }
function say() {
  const [age, height] = arguments
  console.log(`My name is ${this.name}, My age is ${age}, My height is ${height}`)
}
const mySay = say.myBind(myName, 16, 170)
mySay() // My name is Jack, My age is 16, My height is 170

10、instanceof

function myInstanceOf(obj, Fn) {
  // 判斷構(gòu)造函數(shù) Fn 是否出現(xiàn)在 obj 的原型鏈上
  let proto = Object.getPrototypeOf(obj)
  while (proto) {
    if (proto === Fn.prototype) return true
    proto = Object.getPrototypeOf(proto)
  }
  return false
}

11、new

function myNew(Fn, ...args) {
  const obj = new Object()
  obj.__proto__ = Fn.prototype
  // 將構(gòu)造函數(shù)內(nèi)部的 this 替換為新對象,并執(zhí)行構(gòu)造函數(shù)方法
  const res = Fn.apply(obj, args)
  if (typeof res === 'object' && res !== null) {
    // 如果構(gòu)造函數(shù)有返回值,且類型為 object, 則把這個值返回
    return res
  } else {
    return obj
  }
}

12、統(tǒng)計頁面中所有標(biāo)簽的種類和個數(shù)

function getTagCount() {
  // 獲取頁面上所有標(biāo)簽元素
  const tags = document.getElementsByTagName('*')
  const tagNames = []
  for (const val of tags) {
    // 把所有標(biāo)簽名轉(zhuǎn)為小寫,添加到數(shù)組中
    tagNames.push(val.tagName.toLocaleLowerCase())
  }
  const res = {}
  for (const val of tagNames) {
    if (!res[val]) {
      res[val] = 1
    } else {
      res[val]++
    }
  }
  return res
}
// test
console.log(getTagCount()) // { html: 1, head: 1, body: 1, div: 2, script: 1 }

以上就是今天的分享,你是不是全都掌握了呢,歡迎在評論區(qū)交流。如果文章對你有所幫助,不要忘了點上寶貴的一贊!

聽說點贊的人運氣都不差,相信下一個升職加薪的一定是你~??

相關(guān)鏈接:10個常見的前端手寫功能,你全都會嗎?

以上就是前端手寫功能進(jìn)階的詳細(xì)內(nèi)容,更多關(guān)于前端手寫功能的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 微信小程序sessionid不一致問題解決

    微信小程序sessionid不一致問題解決

    這篇文章主要介紹了微信小程序sessionid不一致問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • js實現(xiàn)動態(tài)添加上傳文件頁面

    js實現(xiàn)動態(tài)添加上傳文件頁面

    這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)動態(tài)添加上傳文件頁面,如何動態(tài)創(chuàng)建一個input標(biāo)簽示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • layui 動態(tài)設(shè)置checbox 選中狀態(tài)的例子

    layui 動態(tài)設(shè)置checbox 選中狀態(tài)的例子

    今天小編就為大家分享一篇layui 動態(tài)設(shè)置checbox 選中狀態(tài)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09
  • JS+HTML5實現(xiàn)獲取手機(jī)驗證碼倒計時按鈕

    JS+HTML5實現(xiàn)獲取手機(jī)驗證碼倒計時按鈕

    這篇文章主要介紹了基于JS+HTML5實現(xiàn)獲取手機(jī)驗證碼倒計時按鈕,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • layui時間控件選擇時間范圍的實現(xiàn)方法

    layui時間控件選擇時間范圍的實現(xiàn)方法

    今天小編就為大家分享一篇layui時間控件選擇時間范圍的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09
  • 讓JavaScript 輕松支持函數(shù)重載 (Part 1 - 設(shè)計)

    讓JavaScript 輕松支持函數(shù)重載 (Part 1 - 設(shè)計)

    JavaScript支持函數(shù)重載嗎?可以說不支持,也可以說支持。說不支持,是因為JavaScript不能好像其它原生支持函數(shù)重載的語言一樣,直接寫多個同名函數(shù),讓編譯器來判斷某個調(diào)用對應(yīng)的是哪一個重載。
    2009-08-08
  • 詳解webpack 多入口配置

    詳解webpack 多入口配置

    本篇文章主要介紹了webpack 多入口配置 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • js實現(xiàn)字符串和數(shù)組之間相互轉(zhuǎn)換操作

    js實現(xiàn)字符串和數(shù)組之間相互轉(zhuǎn)換操作

    這篇文章主要介紹了js實現(xiàn)字符串和數(shù)組之間相互轉(zhuǎn)換操作的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-01-01
  • 防止重復(fù)發(fā)送 Ajax 請求

    防止重復(fù)發(fā)送 Ajax 請求

    本文主要介紹防止重復(fù)發(fā)送 Ajax請求的方法。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • JS實現(xiàn)簡單貪吃蛇小游戲

    JS實現(xiàn)簡單貪吃蛇小游戲

    這篇文章為大家詳細(xì)主要介紹了JS實現(xiàn)簡單貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10

最新評論