+ 1
This keyword
Iâm not sure, but does the âThisâ keyword call the object in itself? If not, what does it do?
4 Answers
+ 2
The "this" keyword is used inside classes. It returns a specific value in the object.
This code is a quick demonstration:
https://code.sololearn.com/c5sqJGKknOO5/?ref=app
+ 1
To have an object specific attribute in a class we use this keyword. Suppose that these two lines are in a class:
var s = 5
this.s = 8
If you try to get s value from outside of class with an object, you'll recieve 8 as result. But if you get the s variable with class name you'll recieve 5:
obj.s = 8
class.s = 5
This can be useful when you want each object has it's own value for attributes.
+ 1
Daniel C have a right, but one more , if you have local variable with the same name as atribute - without "this" keyword you adressing to a local variable, when you use "this" keyword - you adressing to atribute.
+ 1
Thanks! All of you really helped.