This is an example of my doubt, why put "this" at the beginning instead of just putting "wheels = 4" or "seats = 1", etc. How can this help me when programming?
var Car = function() {
this.wheels = 4;
this.seats = 1;
this.engines = 1;
};
This is an example of my doubt, why put "this" at the beginning instead of just putting "wheels = 4" or "seats = 1", etc. How can this help me when programming?
var Car = function() {
this.wheels = 4;
this.seats = 1;
this.engines = 1;
};
In JavaScript
this
it works a bit differently than in most object-oriented languages.When used
this
in a function that is not called by any object, it is equivalent to the global objectwindow
.When we make use of
this
inside a function that exists inside aclase
uobjeto
, it refers to theclase
uobjeto
itself.This is a matter of design. In JavaScript it is required to use this in classes to denote that said variable is a property of the class. If you don't use
this
, that variable is stored in the global scope .Correct me if I'm wrong:
I am going to generalize about the keyword
this
since it is used by various programming languages to refer to the "scope" of the element being used.Imagine we have the following code:
There are programming languages whose main focus is on local variables, before anything else, I mean, if the
SetX(X)
following had been put in the code:The compiler wouldn't know what to do, telling you to "assign the value of parameter X to parameter X" . So it
this
refers to the object that could always be present throughout the instance.By doing
this.X = X
so I am telling the compiler to assign the value of my parameterX
into the private variableX
that exists in my class.The same happens with any element, when using the word
this
you are defining that the element you want to call has the lowest priority level, so to speak.Where you could define the priority levels as:
The same thing happens with functions, imagine that there is a function called in the System
Restar()
and you have an element defined with the same name within a class or any other object:By default in any programming language, this element belongs to a class or a higher category object, so if we call from the same instance to
Restar()
, the System function will have more importance than the one you have defined.Lastly, the keyword
this
works as a "pointer" (if it can be called that) to the current instance of the object, so all of the above can work.this : MDN (English) :
As other answers say, keyword
this
is used in various languages. Typically in object-oriented languages with classes (C++ and Java in particular). In these languages,this
it is a reference to "this object", and is used to specify which scope we are referring to when using a variable (or field, or property) name. In Javascript it is similar, but a little more varied and complicated.To a first approximation:
this
it will be inside a function that is part of an object; when we call the function using this object then itthis
will reference that object. Example:If the above is clear, there are a couple of things to note (especially for those of us who come from OOP with classes). First: notice that we haven't declared anything resembling a "person" class, but an object; Contrary to appearances, we cannot (at least not easily) create multiple instances of this "person". Second: we could ask ourselves "Is the reference really
this
resolved at runtime? In fact, here,this
it will always correspond topersona
, right?" No, not necessarily. Example:(This is important to clarify that it is the object that is used to call the function that matters, not the object where the function is "declared".)
Now, there is another slightly different scenario, special and important (in fact, it corresponds to the example in the question). There is a very particular type of functions in Javascript, constructors (the language, unfortunately, does not differentiate between them at a syntactic level - in principle, any function can be used as a constructor - in fact, it is not usually the case - the convention is to use capital letters in name).
The "normal" use of a constructor is with the keyword
new
, which allows you to instantiate various objects (not based on a "class", but on a constructor, which defines a prototype)What happens with the constructor invocation with
new
... important to understand but what is relevant here is the behavior ofthis
: in this case, Javascript first creates a new object and then calls the constructor using that object. So ,this
in that context, points to the newly created object.Finally, what if it is used
this
in a global context, (outside a function, or in a function that is not called as an object property)? In such a case, itthis
points to the "global object" (window
). It is rare that one is interested in this.For more details, similar question on SO (English)