一、map

1.作用:对每个元素加工再返回新集合

2.代码实现:

  1. function myMap(fn, thisArg) {
  2. if(typeof fn !== 'function') throw new Error('callback must be a function')
  3. if(typeof thisArg == undefined) throw new Error('thisArg must not be null or undefined')
  4. let originArr = this,
  5. len = originArr.length;
  6. let result = [];
  7. for (var i = 0;i < len;i++) {
  8. if(i in originArr){
  9. let singleResult = fn.call(thisArg, originArr[i],i,originArr);
  10. result.push(singleResult);
  11. }
  12. }
  13. return result;
  14. }
  15. Array.prototype.myMap = myMap;
  16. let arr = [1, 2, 3];
  17. console.log(
  18. arr.myMap(
  19. function(item) {
  20. return item * 2;
  21. },
  22. { name: "name" }
  23. )
  24. );

3.失误:

(1)遍历数组时用for of

不能把当前元素索引传入回调函数

(2) 忘记给回调函数传入第三个参数-遍历数组本身

(3)没有检查callback和thisArg参数

二、forEach

1.用法

循环遍历数组并将元素传入回调函数然后执行

2.代码实现

  1. function myForEach(fn,thisArg){
  2. let originArr = this,
  3. len = originArr.length;
  4. for(var i = 0;i<len;i++){
  5. if(i in originArr){
  6. fn.call(thisArg,originArr[i],i,originArr)
  7. }
  8. }
  9. }
  10. Array.prototype.myForEach = myForEach
  11. let arr = [1,2,3];
  12. arr.myForEach(function(item,index){
  13. console.log(item,index,'iterator')
  14. },{name:'my thisArg'})

三、filter

1.用法

遍历数组,将元素传入回调函数并执行,返回符合条件的元素合集

2.代码实现

  1. function myFilter(callback, thisArg) {
  2. if (typeof callback !== "function") {
  3. throw new Error("callback must be a function");
  4. }
  5. if (typeof thisArg == null) {
  6. throw new Error("thisArg must not be null or undifined");
  7. }
  8. let originArr = this,
  9. len = this.length;
  10. let result = [];
  11. for (var i = 0; i < len; i++) {
  12. if(i in originArr){
  13. let passed = callback.call(thisArg, originArr[i], i, originArr);
  14. if (passed) result.push(originArr[i]);
  15. }
  16. }
  17. return result;
  18. }
  19. Array.prototype.myFilter = myFilter;
  20. let arr = [1, 2, 3];
  21. console.log(
  22. arr.myFilter(
  23. function(item) {
  24. return item > 1;
  25. },
  26. { name: "name" }
  27. )
  28. );

四、reduce

1.用法

遍历数组累计计算返回结果
参数:callback和initialValue

2.代码

  1. function myReduce(callback,initialValue){
  2. if(typeof callback !== 'function') throw new Error('callback must be a function')
  3. let originArr = Object(this),
  4. k = 0,
  5. len = originArr.length;
  6. let accumulotar = initialValue;
  7. console.log(accumulotar)
  8. if(accumulotar === undefined){
  9. while(k < len && !(k in originArr)){
  10. k++
  11. }
  12. if(k >= len){
  13. throw new Error('Reduce of empty array with no initial value')
  14. }
  15. accumulotar = originArr[k++]
  16. }
  17. while(k < len){
  18. if(k in originArr){
  19. console.log(k)
  20. accumulotar = callback.call(undefined,accumulotar,originArr[k],k,originArr)
  21. }
  22. k++
  23. }
  24. return accumulotar
  25. }
  26. Array.prototype.myReduce = myReduce
  27. let arr = new Array(2);
  28. arr.push(1)
  29. arr.push(2)
  30. console.log(arr.myReduce((accumulotar,element)=>{return accumulotar + element},8))

五、Function.prototype.apply

1.用法

改变函数执行上下文this指向,借用函数(Object.prototype.toString, 构造函数借用)

2.代码

  1. function myApply(thisArg = window, ...args) {
  2. let originFunction = this;
  3. if (typeof originFunction !== "function") {
  4. throw new Error("type error");
  5. }
  6. let functionName = Symbol();
  7. thisArg[functionName] = originFunction;
  8. return thisArg[functionName](args);
  9. }
  10. Function.prototype.myApply = myApply

六、模拟new关键字

1.用法

(1)先一个新对象并继承构造函数原型
(2)借用构造函数给新对象添加属性
(3)根据构造函数中的返回值,来判断返回新对象,还是构造函数的返回值

2.代码

  1. function myNew(constructor,...args){
  2. let instance = Object.create(constructor.prototye),
  3. result = constructor.apply(instance,args),
  4. resultType = typeof result;
  5. let isObject = resultType === 'object' && !== null,
  6. isFunction = resultType === 'function';
  7. return isObject || isFunction ? result : instance
  8. }