Im curious when it is needed/best practice to use the keyword this
. I understand that this
is used when determining a functions this
value but is it always needed?
The reason I am asking is because I have a internal function and it is called within my module and all it really does is sort some data you pass it. My question is should I call this function using the this
keyword or stand alone.
E.g:
function formatSomeData(data){
//code........
}
this.formatSomeData(data);
OR
formatSomeData(data);
I get that the context of where the function is being called and what its purpose is matters in answering the question, but in this case like I mentioned, I really don't need to access the this
object at any point. Is it still good practice to use it when calling functions?
What I'm asking is not so much as to how "this" works, but when is it appropriate to use it and when isn't it.
This is usually used when you want to access something in some. So for instance, if you have a custom object and want to use some property inside some method, you should use
this
.If you see, in current constructor, I created a function(
fullName
) which needs to accessfname
andlname
properties of the object it is part of. This is a placethis
must be used.Any property in a constructor that is a part of
this
will be a part of the object. So if you need something that is only accessible to you but not outside, you can usefunction
instead ofthis
.As for your code, I have already explained in comment why it works:
But as rightly pointer by @Jonas W its a bad practice. Why it is bad?
var|let|const
) will become part of global scope.Programming is a way to express things so that computers and humans can understand them.
There is no exact answer. Both of your snippets do work. So the computer is able to understand it. However the second one is maybe confusing for humans (aka your coworkers):
What should
this
refer to? Yes technically it refers towindow
, but from a human point of view it is unclear. Thats why i would definetly not refer tothis
in this case. However there are other good usecases for it:TLDR: Use
this
to refer to methods/properties, and leave it away when refering to pure functions and variables.'This' is used inside a function and it contains the value of the object that invokes the function.
For example:
In this case, even though we just define newVariable inside the function a(). The object that invokes the function is the global object window, so 'this' points to window and this.newVariable is at the global scope.
Another example would be:
In this case, 'this' points to the object c, since the log function will be invoked by c.
We have more tricky cases in the use of the object 'this' in Javascript. This is a good article to grasp the concept: http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/
For a starting JS developer it might be easiest to get concept of This by using it in an event listener callback. I. e., "click" event binds This to object which is the target (Like "look, i clicked on THIS!").
Of course Rajeshs answer is much, much more complex, but I hope THIS can be also helpful…