2

A couple months ago I joined the Odin Project and am currently working on a project for which I reached out for guidance.

It was brought to my attention that I should not use the "this" keyword because it is a "slippery slope" and that I should use event.target instead.

My question is why... Why is this a slippery slope and why is it bad to use?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 6
    `this` is equivalent to `event.currentTarget`, not `event.target`. I don't know what "slippery slope" they're talking about. – Barmar Mar 01 '20 at 06:10
  • 1
    They could be referring to the way "this" works in JavaScript. "this" could have a different value depending what trigger the function enclosing the "this" reference. So in most cases, it is good practice to use event variable (maybe they were wrong about the currentTarget/Target). This is a good read on that subject - https://stackoverflow.com/a/3127440/8161471 – Alejandro Mar 01 '20 at 06:21
  • @Alejandro Thank you for the resource! Barmar this is exactly why I was confused. But I figured this would be the best place for an answer. – Dave Schuster Mar 01 '20 at 11:53

1 Answers1

1

Because, within the event handler, the value of "this" might be confused for another value, depending on whether you have nested functions, for example. Using the value from the event instance, which is the arg of the event-handling function, cannot be confused for another value (unless you change it, of course).

Hari Lubovac
  • 622
  • 4
  • 14