1

Beginner-ish question:

Many of the JavaScript native array methods (map, filter, find, forEach, etc) can be reassigned by passing a "this" object to the second argument. I've never seen that used and I'm struggling to come up with any situation where it would be needed.

Every array gets all the native methods by default, so when would it be useful to rebind them?

Matthew Masurka
  • 325
  • 3
  • 14
  • 1
    Go through this post. I think this is duplicate question https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback/54439040#54439040 – Ashish Feb 07 '19 at 03:54
  • 2
    Not really _"reassigned"_. The _`thisArg`_ lets you assign the `this` context for the callback function, otherwise it is `undefined`. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description – Phil Feb 07 '19 at 04:03
  • 1
    Highly recommend viewing https://egghead.io/courses/understand-javascript-s-this-keyword-in-depth course. It will give very good insight into how `this` works. – Hozefa Feb 07 '19 at 04:22
  • 2
    As Phil says, the second argument is for setting the `this` value of the callback, not the array method itself. – Felix Kling Feb 07 '19 at 04:50
  • Gotcha - sorry, missed that in the docs. Thanks! – Matthew Masurka Feb 07 '19 at 06:41

0 Answers0