手撕js中常见方法

array.fill

javascript 复制代码
Array.prototype.fill2 = function (value, start, end) {
  const length = this.length; // 获取数组的长度

  start = start >> 0; // 将start转换为整数
  // 如果end参数未填写,则默认为数组的长度,否则将end转换为整数
  end = typeof end === 'undefined' ? length : end >> 0;
  // 将start限制在合法范围内,最小值为0,最大值为数组的长度
  start = start >= 0 ? Math.min(start, length) : Math.max(start + length, 0);
  // 将end限制在合法范围内,最小值为0,最大值为数组的长度
  end = end >= 0 ? Math.min(end, length) : Math.max(end + length, 0);
  // 使用指定的value填充从start到end范围内的数组索引
  while (start < end) {
    this[start] = value;
    start++;
  }
  // 返回被修改后的数组
  return this;
}

这段代码实现了一个自定义的fill2方法,用于在数组中指定的范围内填充指定的值。它接受三个参数:value表示要填充的值,start表示填充的起始索引,end表示填充的结束索引(不包含在内)。

在代码中,首先获取了数组的长度。然后对start和end参数进行处理,将它们转换为整数,并进行范围限制,确保它们在合法的范围内。接下来,使用一个循环从start索引开始,逐个将数组的元素设置为指定的value值,直到达到end索引为止。最后,返回被修改后的数组。

通过使用这个自定义的fill2方法,我们可以在数组中指定的范围内快速填充指定的值。

array.map

javascript 复制代码
Array.prototype.myMap = function (callbackFn, thisArg) {
  if (this == "undefined" || this == "null") {
    throw new TypeError("can not read property 'map' of undefined or null");
  }
  if (typeof callbackFn !== "function") {
    throw `${callbackFn} is not a function`;
  }
  let O = Object(this); // 将当前数组对象转换为对象类型
  let T = thisArg; // 设置回调函数的this值
  let len = O.length >>> 0; // 获取数组的长度
  let A = new Array(len); // 创建一个新的数组,用于存储映射后的值
  for (let k = 0; k < len; k++) {
    for (k in O) {
      let KValue = O[k]; // 获取当前索引的值
      let mapValue = callbackFn.call(T, KValue, k, O); // 调用回调函数进行映射操作
      A[k] = mapValue; // 将映射后的值存储到新数组中
    }
  }
  return A; // 返回映射后的新数组
}

let arr = [1, 2, 3, 4];
let arr2 = arr.myMap(function (currentValue, index, Array) {
  console.log(currentValue, index, Array);
  return currentValue + 1;
});
console.log(arr2);
javascript 复制代码
    Array.prototype.myMap = function (callbackFn, thisArg) {
        if (this == "undefined" || this == "null") {
            throw new TypeError("can not read property 'map' of undefined or null")
        }
        if (typeof callbackFn !== "function") {
            throw `${callbackFn} is not a function`
        }
        let length = Object(this).length >>> 0
        let newArrray = []
        let i = 0
        while (i < length) {
            if (i in this) {
                newArrray.push(callbackFn.call(thisArg, this[i],i,this))
            }
            i++
        }
        return newArrray
    }

    let arr = [1,2,3,4]
    let arr2 = arr.myMap(function(currentValue,index,Array){
        console.log(currentValue,index,Array,this);
        return ++currentValue
    },{message:"🐟"})
    console.log(arr2);
相关推荐
jinzeming9993 分钟前
Vue3 PDF 预览组件设计与实现分析
前端
NuLL3 分钟前
全场景智能克隆工具:超越 JSON.parse(JSON.stringify())
javascript
编程小Y4 分钟前
Vue 3 + Vite
前端·javascript·vue.js
GDAL13 分钟前
前端保存用户登录信息 深入全面讲解
前端·状态模式
大菜菜20 分钟前
Molecule Framework -EditorService API 详细文档
前端
Anita_Sun21 分钟前
😋 核心原理篇:线程池的 5 大核心组件
前端·node.js
认真敲代码的小火龙24 分钟前
【JAVA项目】基于JAVA的养老院管理系统
java·开发语言·课程设计
灼华_25 分钟前
Web前端移动端开发常见问题及解决方案(完整版)
前端
_请输入用户名26 分钟前
Vue3 Patch 全过程
前端·vue.js
孟祥_成都26 分钟前
nest.js / hono.js 一起学!字节团队如何配置多环境攻略!
前端·node.js