Hi there - Quick question regarding Implement map on a Prototype exercise. How/why is “this” referencing the array. How does the computer know that? Was something done on the FCC backend or is this just how things work? The instructions do indicate that " The Array
instance can be accessed in the myMap
method using this
." I’m just not sure if something was done in the backend for this to happen.
Any guidance is appreciated!
Your code so far
// The global variable
var s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback) {//callback is a function
var newArray = [];
// Only change code below this line
for (var i = 0; i < this.length; i++){
newArray.push(callback(this[i]));
}
//this.forEach(x => newArray.push(callback(x)));
// Only change 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 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Challenge: Implement map on a Prototype
Link to the challenge: