Implement map on a Prototype(this property)

Tell us what’s happening:
I want to know how this property is refrencing newArray in my code?

Your code so far


// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  for(let i=0; i<s.length; i++){
    this.push(callback(s[i]));    //HOW??????????????????
  }
  // Add your code above this line
  return newArray;

};

var new_s = s.myMap(function(item){
  return item * 2;
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype

Uhm if i am not mistaken your this is NOT a reference to newArray: it is a reference to s because of s.myMap

Yes you are right…

1 Like