The This Keyword-(Implement the filter Method on a Prototype)

Hi,
I can’t figure out how the “this” keyword is working here. I tried this code with “callback.forEach(…” rather than “this.forEach(…” and it did not work. What does “this” point to here, if it’s not the “callback” argument?
Thanks in advance for any help.


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

Array.prototype.myFilter = function(callback){
  var newArray = [];
  // Add your code below this line
  this.forEach( (e) => {if (callback(e)){newArray.push(e)} });
  // Add your code above this line
  return newArray;

};

var new_s = s.myFilter(function(item){
  return item % 2 === 1;
});

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

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

this refers to the object (the array in this case) in which the method is called on. See a previous lesson on this.

So, “this” is pointing to the array “s”, whereas “callback” would point to the function that was passed in?

Cool. I think I understand it now. Thank you very much for the help!