Functional Programming: Implement Map on a Prototype - start with 'this'

Here is another “OMG where do I start!?” - which is good I guess, because I get the feeling that there’s going to be a log of that ‘in the field’. I don’t remember encountering Array.prototype.forEach() in the curriculum, but it’s easy enough to look up. The difficulty here seems to be ‘how do I refer to each element in whatever array is passed in if there’s no reference to it in the parameters of Array.prototype.myMap()?’ I read and saw others talking about the ‘this’ keyword, but was still confused until I typed ‘this’ inside the body of Array.prototype.myMap:

console.log(this);

Which was the golden ‘aHA!’ moment. It lets you know that ‘this’ in the context of this particular problem, refers to the array, s, and in other problems, whatever array is passed in originally. From there, it becomes possible to figure out. Hope this helps others who may be confused like I was.

For posterity: Here is the freeCodeCamp challenge that introduces this

1 Like