+ 7
How to use 'this' javascript
I don't understand what is this used for, could you explain me please
6 Respostas
+ 12
The keyword 'this' refers to the current object.
Example:
function Cat(n, c) {
this.name = n;
this.color = c;
}
Cat1 = new Cat("Garfield", "orange");
Cat2 = new Cat("Donald", "orange");
The value "Garfield" can be accessed when calling Cat1.name, while Cat2.name gives us "Donald". When we wrote the constructor for Cat, we did not know, that the objects would be named Cat1 or Cat2, but with 'this' we can still access the current object, whatever its name might be.
I'd recommend you read the chapter on objects again and of you don't understand everything, ask a more precise question, where you tell us, what it is you do not understand.
+ 16
the 'this' keyword, in javascript is used in a class to refer to an instance of the class.
for example:
function people(name, age){
this.name = name;
this.age = age;
}
with the class it is possible to create multiple objects. In each object the keyword referes to that object.
+ 13
Can you post a link for the code?
+ 2
i was asking for the general purpose
If I use this inside a function, what is equivalent to it without this?
When is 'this' useful?
+ 2
thanks bro