常用前端手写功能进阶示例详解

目录
  • 1、Promise.all
  • 2、Promise.race
  • 3、Promise.any
  • 4、冒泡排序
  • 5、选择排序
  • 6、快速排序
  • 7、call
  • 8、apply
  • 9、bind
  • 10、instanceof
  • 11、new
  • 12、统计页面中所有标签的种类和个数

1、Promise.all

Promise.myAll = function (promises) {
  return new Promise((resolve, reject) => {
    // promises 可以不是数组,但必须要具有 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 对象转为 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 可以不是数组,但必须要具有 Iterator 接口
    if (typeof promises[Symbol.iterator] !== 'function') {
      reject('TypeError: promises is not iterable')
    }
    for (const item of promises) {
      // 先出来的结果会被 resolve 或 reject 出去, 一旦状态变化就不会再变
      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 可以不是数组,但必须要具有 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]]
      }
    }
    // 每次遍历结束,都能找到一个最大值,放在数组最后
  }
  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++) {
    // 假设每次循环,最小值就是第一个
    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
  // 每次取第一个元素作为基准值
  const pivot = arr.shift()
  const left = []
  const right = []
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < pivot) {
      // 如果小于基准值,则把它放在左数组
      left.push(arr[i])
    } else {
      // 如果大于等于基准值,则放在右数组
      right.push(arr[i])
    }
  }
  // 递归处理,并把左中右三个数组拼接起来
  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]调用的时候,内部的 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]调用的时候,内部的 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 调用,this 为新创建的对象实例,将函数内部的 this 替换为这个新对象
      fn.apply(this, newArgs)
    } else {
      // 普通方式调用,将函数内部的 this 替换为 context
      fn.apply(context, newArgs)
    }
  }
  // 支持 new 调用
  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) {
  // 判断构造函数 Fn 是否出现在 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
  // 将构造函数内部的 this 替换为新对象,并执行构造函数方法
  const res = Fn.apply(obj, args)
  if (typeof res === 'object' && res !== null) {
    // 如果构造函数有返回值,且类型为 object, 则把这个值返回
    return res
  } else {
    return obj
  }
}

12、统计页面中所有标签的种类和个数

function getTagCount() {
  // 获取页面上所有标签元素
  const tags = document.getElementsByTagName('*')
  const tagNames = []
  for (const val of tags) {
    // 把所有标签名转为小写,添加到数组中
    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 }

以上就是今天的分享,你是不是全都掌握了呢,欢迎在评论区交流。如果文章对你有所帮助,不要忘了点上宝贵的一赞!

听说点赞的人运气都不差,相信下一个升职加薪的一定是你~

相关链接:10个常见的前端手写功能,你全都会吗?

以上就是前端手写功能进阶的详细内容,更多关于前端手写功能的资料请关注我们其它相关文章!

(0)

相关推荐

  • 自己动手写的javascript前端等待控件

    等待控件在网上搜有好多种,但是都很复杂,不一定用的顺手,再说我的项目是bootstrap的原因,又不敢轻易使用第三方控件,怕不兼容,于是自己动手写了个等待控件,其技术点包括动态加载CSS,javascript的命名空间,所以记录一下. 这个等待控件主要是:进行某个操作前,显示一个信息提示:"数据加载中,请稍候...",操作成功后,在回调函数中将提示消失,原理是这个等待控件完全由JS动态加进去,包括CSS,页面中并无预先设定. 那么这个CSS怎么动态加载呢?等待控件中,样式使用了clas

  • 「中高级前端面试」JavaScript手写代码无敌秘籍(推荐)

    1. 实现一个new操作符 new操作符做了这些事: 它创建了一个全新的对象. 它会被执行[[Prototype]](也就是__proto__)链接. 它使this指向新创建的对象.. 通过new创建的每个对象将最终被[[Prototype]]链接到这个函数的prototype对象上. 如果函数没有返回对象类型Object(包含Functoin, Array, Date, RegExg, Error),那么new表达式中的函数调用将返回该对象引用. function New(func) { va

  • 分享10个常见的JavaScript前端手写功能

    目录 1.防抖 2.节流 3.深拷贝 4.手写 Promise 5.异步控制并发数 6.继承 7.数组排序 8.数组去重 9.获取 url 参数 10.事件总线 | 发布订阅模式 1.防抖 function debounce(fn, delay) {   let timer   return function (...args) {     if (timer) {       clearTimeout(timer)     }     timer = setTimeout(() => {  

  • 前端面试JavaScript高频手写大全

    目录 1. 手写instanceof 2. 实现数组的map方法 3. reduce实现数组的map方法 4. 手写数组的reduce方法 5. 数组扁平化 5. 1 es6提供的新方法 flat(depth) 5.2 利用cancat 6. 函数柯里化 7. 浅拷贝和深拷贝的实现 7.1浅拷贝和深拷贝的区别 8. 手写call, apply, bind 8.1 手写call 8.2 手写apply(arguments[this, [参数1,参数2.....] ]) 8.3 手写bind 9.

  • 高级前端面试手写扁平数据结构转Tree

    目录 前言 什么是好算法,什么是坏算法 时间复杂度 计算方法 空间复杂度 计算方法: 不考虑性能实现,递归遍历查找 不用递归,也能搞定 最优性能 小试牛刀 前言 招聘季节一般都在金三银四,或者金九银十.最近在这五六月份,陆陆续续面试了十几个高级前端.有一套考察算法的小题目.后台返回一个扁平的数据结构,转成树. 我们看下题目:打平的数据内容如下: let arr = [ {id: 1, name: '部门1', pid: 0}, {id: 2, name: '部门2', pid: 1}, {id:

  • 常用前端手写功能进阶示例详解

    目录 1.Promise.all 2.Promise.race 3.Promise.any 4.冒泡排序 5.选择排序 6.快速排序 7.call 8.apply 9.bind 10.instanceof 11.new 12.统计页面中所有标签的种类和个数 1.Promise.all Promise.myAll = function (promises) { return new Promise((resolve, reject) => { // promises 可以不是数组,但必须要具有 I

  • SQL实现Excel的10个常用功能的示例详解

    目录 01. 关联公式:Vlookup 02. 对比两列差异 03. 去除重复值 04. 缺失值处理 05. 多条件筛选 06. 模糊筛选数据 07. 分类汇总 08. 条件计算 09. 删除数据间的空格 10. 合并与排序列 SQL笔试题原题 某数据服务公司 某手游公司的SQL笔试题(原题) 某互联网金融公司SQL笔试题(原题) SQL,数据分析岗的必备技能,你可以不懂Python,R,不懂可视化,不懂机器学习.但SQL,你必须懂.要不然领导让你跑个数据来汇......,哦不,你不懂SQL都无

  • go语言定时器Timer及Ticker的功能使用示例详解

    目录 定时器1-"*/5 * * * * *" 设置说明 定时器2-Timer-Ticker Timer-只执行一次 Ticker-循环执行 Timer延时功能 停止和重置定时器 定时器Ticker使用 定时器1-"*/5 * * * * *" package main import ( "fmt" "github.com/robfig/cron" ) //主函数 func main() { cron2 := cron.New

  • Python实现邮件发送功能的示例详解

    想实现发送邮件需要经过以下几步: 1.登录邮件服务器 2.构造符合邮件协议规则要求的邮件内容 3.发送 Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件,它对smtp协议进行了简单的封装. 这里我们用qq邮箱为例,并且自己是可以给自己发邮件的. 在开始前我们先做准备工作: 登录qq邮箱,后点击“设置” 点击“账户” 确保前两项已开启,然后点击生成授权码. 因为我们网页登录时的密码是不可以用来python上使用:qq为了安全,我们平

  • 比ant更丰富Modal组件功能实现示例详解

    目录 有哪些比ant更丰富的功能 render部分 渲染黑色蒙层 渲染弹框主体 设置body overflow:hiiden 有哪些比ant更丰富的功能 普通的modal组件如下: 我们写的modal额外支持,后面没有蒙版,并且Modal框能够拖拽 还支持渲染在文档流里,上面的都是fixed布局,我们这个正常渲染到文档下面: render部分 <RenderDialog {...restState} visible={visible} prefixCls={prefixCls} header={

  • vue项目网站全局置灰功能实现示例详解

    目录 1.前端独立实现 2.通过后台管理控制设置网站的整体置灰样式 1.前端独立实现 两种不同的逻辑,一种是前端自己实现,一种是结合后台管理系统来控制,网站是正常还是置灰. 直接在public文件夹下的index.html文件的html标签里加上style="filter:grayscale(100%)",总的就是<html style="filter:grayscale(100%)"> 2.通过后台管理控制设置网站的整体置灰样式 1.先给public文

  • Swift中的高阶函数功能作用示例详解

    目录 高阶函数的作用 1. 简化代码 2. 提高可读性 3. 支持函数式编程 4. 提高代码的可重用性 常见的高阶函数 1. map() 2. filter() 3. reduce() 4. sorted() 5. forEach() 6. compactMap() 7. flatMap() 8. zip() 9. first() 10. contains() 高阶函数的作用 Swift中的高阶函数是指那些参数或返回值是函数的函数.它们的存在使得我们可以用非常简洁和优雅的代码来解决许多问题. 1

  • Java实现图片裁剪功能的示例详解

    目录 前言 Maven依赖 代码 验证一下 前言 本文提供将图片按照自定义尺寸进行裁剪的Java工具类,一如既往的实用主义. Maven依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency> <dependen

  • Ajax实现上传图像功能的示例详解

    最终效果展示 xhr发起请求 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="widt

随机推荐