手撕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);
相关推荐
mCell13 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip13 小时前
Node.js 子进程:child_process
前端·javascript
excel16 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel18 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼19 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping19 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙20 小时前
[译] Composition in CSS
前端·css
白水清风20 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix20 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户221520442780020 小时前
new、原型和原型链浅析
前端·javascript