0
3

[–] Cuddlefluff 0 points 3 points (+3|-0) ago 

This is generally the object you called the function on

someObject.func() in this case this refers to the someObject object.

However, this is not always the case, but that's the rule you should follow. If you call a function in a global scope (not on an object) this refers to window.

The reason I say it's not "always the case" is because sometimes the context might be different, and this refers an object that is not necessarily the one you called it on (for instance for some asynchronous methods), but what you'd "expect" this to refer to. Sometimes you need to redirect the this reference to something else, and that's done with someFunction.Call(whatEverYoudLikeTheThisReferenceToBeAssignedTo)

JavaScript is very different than other object oriented languages in this case, because JavaScript is not an object oriented language (I know a lot of people will disagree with me on that, but it really isn't)

I don't know if I cleared anything up, or if I made things just harder to understand, so I'll just reiterate :

this refers to the object you called a function on. If you called a function in a global scope (not part of an object) this refers to window. It's that simple.

0
0

[–] tragicwhale [S] ago 

I'm going to read your comment over and over but I feel like the portion where you talk about it refering to window helped. Thanks for your response.

0
0

[–] Cuddlefluff ago  (edited ago)

I'm not any good at explaining things, but it's really quite easy once you "get" it. I have a long career of object oriented programming (C++, Java and C#), and this also confused me a great deal in JavaScript in the beginning. It's not as intuitive and obvious as you'd expect. But it all boils down to whether or not you wrote a . - this would refer to the object on the left-hand side of the ., If there is no . it would refer to the global scope aka window. That's it, I don't know if I'm able to make it any clearer than that :P

[–] [deleted] 0 points 1 point (+1|-0) ago  (edited ago)

[Deleted]

0
1

[–] tragicwhale [S] 0 points 1 point (+1|-0) ago 

Hey Rodrigo, thanks for the examples, those will definitely be in my notes and help clarify some things. Very much appreciate it.