Full Discussion (The ‘this’ keyword in Javascript)

You didn't really touch the tricky parts of this. Like for example, what happens when you try to pass around a function that refers to this:

const o = {
  f: function() { console.log(this); }

o.f(); // prints o, no problems!

const g = o.f; // passed around the function instance, this could be passing to an event handler or some thing

g(); // prints window, oops. 

The main problem with this isn't that you need to pass it around in a parameter or that you might accidentally define things outside the scope.

The main problem with this is that this is determine when the function is called and how the function is called, in the example here, this changed because I no longer called f() with o behind the dot operator. So it defaults to the global object.

code of conduct - report abuse