How do higher-order functions like `map()` and `reduce()` receive their data?

You can add it to the Array prototype like :

Array.prototype.myOwnFunction = function() {
  for (var i = 0; i < this.length; i++) {
    this[i] += 1;
  }

  return this;
};

const array = [1, 2, 3];

const result = array.myOwnFunction();

console.log(result);


Check the polyfill for Array.prototype.map(), this line in particular:

//  1. Let O be the result of calling ToObject passing the |this| 
//    value as the argument.
var O = Object(this);

Simplifying, this is where the values are received.

Tags:

Javascript