+ 7

How to use 'this' javascript

I don't understand what is this used for, could you explain me please

27th Apr 2017, 11:49 PM
Victor_*
Victor_* - avatar
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.
28th Apr 2017, 2:07 AM
Tob
Tob - avatar
+ 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.
28th Apr 2017, 2:11 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 13
Can you post a link for the code?
28th Apr 2017, 1:41 AM
Ulisses Cruz
Ulisses Cruz - avatar
28th Apr 2017, 2:55 AM
Benneth Yankey
Benneth Yankey - avatar
+ 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?
28th Apr 2017, 2:00 AM
Victor_*
Victor_* - avatar
+ 2
thanks bro
28th Apr 2017, 2:09 AM
Victor_*
Victor_* - avatar