This keyword | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

This keyword

I’m not sure, but does the “This” keyword call the object in itself? If not, what does it do?

2nd Nov 2019, 9:27 PM
Jake
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
2nd Nov 2019, 9:36 PM
Daniel C
Daniel C - avatar
+ 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.
2nd Nov 2019, 10:16 PM
Qasem
+ 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.
3rd Nov 2019, 9:28 PM
id001x
id001x - avatar
+ 1
Thanks! All of you really helped.
5th Nov 2019, 1:14 AM
Jake