Why do we need to use 'this' here?

Tell us what’s happening:
Why must this be used when just leaving s in works just as well?

  **Your code so far**

// The global variable
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback) {
// Only change code below this line
var newArray = [];
for(var i = 0; i < s.length; i++) {
  if(callback(s[i]) === true) {
    newArray.push(s[i]);
  }
}
// Only change 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 (X11; CrOS x86_64 13421.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.199 Safari/537.36.

Challenge: Implement the filter Method on a Prototype

Link to the challenge:

Hello there,

Well, think of what happens when I want to re-use your function (essentially the whole point of creating functions - re-use):

var myArr = [1, 3, 4, 5];

var new_arr = myArr.myFilter(function (x) {
  return x % 2 === 1;
});
console.log(new_arr);

Run that code, and see if you get what you expect…


To explain it with more examples:

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [5, 6, 7, 8, 9];

console.log(arr1.filter(x => x % 2 === 0)); // [2, 4]
console.log(arr2.filter(x => x % 2 === 0)); // [6, 8]

// Weird example, but puts 'this' in perspective:
console.log(arr1.forEach(x => {
  if (x % 2 === 0) {
    console.log(this); // 'this' refers to arr1
  };
}));
console.log(arr2.forEach(x => {
  if (x % 2 === 0) {
    console.log(this); // 'this' refers to arr2
  };
}));

Hope this helps

I dunno…the first one says myArr.myFilter is not a function. The second one returns [2,4] [6,8] once…then every time i try to run it in my browser after that it says Uncaught SyntaxError: Identifier ‘arr1’ has already been declared. Still kinda lost.

you need to put it after you create myFilter method