Question of "this" at implement map

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++){
// 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

Challenge: Implement map on a Prototype

Link to the challenge:

1 Like

Good question. I am still trying to grasp “this”, I haven’t understood the concept completely.

Hi, thank you for this video, I will check it!

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.

1 Like

Hi, thanks for replying.
However, I’m wondering what callback(this[i]) means.
Why can’t I use callback[i] instead?

callback is a function, not an array or string. So it needs to be used like a function : callback(argument) { do stuff with argument }

callback[anythingHere] is not valid. I can expand further on my answer if needed. If so, answer what you expect i to be here:

In case of Class this refers to the object created using the class. For example

class Vehicle {
       constructor (wheels,color){
             this.wheels = wheels;
             this.color = color

const  car = new Vehicle(4,"red")

In this case the this keyword inside Vehicle class refers to the car object. Its same as

const car={}
car.wheel = 4;
car.color = "red"

here this refers to the array s. so callback(this[i]) is equal to callback (s[i])

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.