Use of this on "Implement map on a Prototype" exercise

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:

1 Like

well to answer that , you’d have to know what .prototype is, its better if you look into this for yourself and see if that clears thing up or not!! this might not be kind of response you were expecting but it would help you in long run!! happy coding :slight_smile:

3 Likes

Noted. Thanks. Will make sure to read about prototype :blush:

2 Likes

The challenge here is two parts: what is a prototype, and what is meant by this?

There are a few objects javascript understands a little differently than you or i. When we see a number, javascript sees “a primitive that we wrap in the Number constructor.” When we see a string, javascript sees “a primitive that we wrap in the String constructor.” And where we see an array, javascript sees “an object that gets wrapped with the Array constructor.”

The following two lines are the same, in essence:

let str1 = "snowmonkey likes to code.";
let str2 = new String("snowmonkey likes to code.");

But why does that matter? Because, by wrapping a string in the String constructor, javascript lets us do:

console.log("snowmonkey".toUpperCase() );

That toUpperCase() is not a method we have added to our string - it comes from the String in which it has been wrapped. It is a method defined on the String.prototype, which all things String can access.

So what is this, and how does it connect to the prototype? this is a special javascript keyword, that points to the execution context. When we call:

"sharknado!!".toUpperCase()

The execution context of that, the object actually calling toUpperCase(), is the string itself. The method defined as String.prototype.toUpperCase() is running on the context of (and with the this of) our string itself.

Whenever we use number functions or string methods or array methods, ones like toFixed() or string.split(" ") or arr.join("-") we are using methods defined on the prototype of that type of thing, in the context of the particular instance we’ve attached it to.

This in no way contradicts what @bappyasif suggested - learn about what they are and how they work. This is a high-level overview, pointing out some interesting landmarks, but you’ll truly benefit by reading more about prototypes and context. I’d highly recommend the You Don’t Know Javascript (Yet) books, available on getify.

3 Likes

Thanks you so much for this overview. I appreciate it!!! Yes, i will definitely read about prototypes. Thanks for the books recommendations! Much appreciated.

1 Like

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