Tell us what’s happening:
Describe your issue in detail here.
Dear all,
I have problem with the solution 1 of this course, I’m wondering what is the “this” means inside the for loop. Thanks
**Your code so far**
// The global variable
var s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback) {
var newArray = [];
// Only change code below this line
for(let i = 0; i < this.length; i++){
newArray.push(callback(this[i]))
}
// 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_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15
Creates a method that can be used on all arrays in your code and takes a callback (function) that is executed on the array which myMap is called on.
This calls myMap on the array s and assigns it to a variable. s calls myMap so this refers to s. Everywhere this is used refers to s. You could also call myMap on any array in this code: ["my", "array"].myMap(aFunction) and then this would refer to ["my", "array"]. Using this makes the code reusable.