手撕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);
相关推荐
Kakarotto2 小时前
Canvas 直线点击事件处理优化
javascript·vue.js·canvas
顺遂2 小时前
基于Rokid CXR-M SDK的引导式作业辅导系统设计与实现
前端
代码搬运媛2 小时前
Generator 迭代器协议 & co 库底层原理+实战
前端
前端拿破轮2 小时前
从0到1搭建个人网站(三):用 Cloudflare R2 + PicGo 搭建高速图床
前端·后端·面试
功能啥都不会2 小时前
PM2 使用指南 - 踩坑记录
前端
HelloReader2 小时前
React 中 useState、useEffect、useRef 的区别与使用场景详解,终于有人讲明白了
前端
兆子龙2 小时前
CSS 里的「if」:@media、@supports 与即将到来的 @when/@else
前端
踩着两条虫2 小时前
AI 智能体如何重构开发工作流
前端·人工智能·低代码
代码老中医2 小时前
逃离"Div汤":2026年,当AI写了75%的代码,前端开发者还剩什么?
前端
进击的尘埃2 小时前
Playwright Component Testing 拆到底:组件怎么挂上去的,快照怎么在 CI 里不翻车
javascript