zoukankan      html  css  js  c++  java
  • js 手写 map 函数

    一 map 函数(copyMap)

    map函数接收两个参数

    1 迭代器函数 ,该函数有三个参数

    • 数组项的值
    • 数组项下标
    • 数组对象本身

    2 迭代器函数的this指向
    (注:当传了该值,迭代器函数不能为箭头函数了。原因是箭头函数没有this隐式指向。箭头函数在定义时候就已经绑定了上层上下文中非箭头函数this)

    Array.prototype.copyMap = function (fn, toThis) {
      let arr = this;
      const result = [];
      const redirectThis = toThis || Object.create(null);
      for (let i = 0; i < arr.length; i++) {
        const item = fn.call(redirectThis, arr[i], i, arr);
        result.push(item);
      }
      return result;
    };
    
  • 相关阅读:
    内置方法(item系列、__str__方法、__del__方法)
    POJ3436
    CF551B
    HDU1588
    HDU3117
    CF834D
    CF832D
    CF832C
    POJ1930
    POJ3666
  • 原文地址:https://www.cnblogs.com/honkerzh/p/14088828.html
Copyright © 2011-2022 走看看