What "this" can do in js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What "this" can do in js?

29th Apr 2020, 3:37 AM
Алинур Кадыр уулу
Алинур Кадыр уулу - avatar
2 Answers
0
The JavaScript this keyword refers to the object it belongs to. In object method this refers to the owner of the method. It's values depending on where it is used. Let's say there is the method object called person as var person = { firstName: "John", lastName : "Doe", fullName : function() { return this.firstName + " " + this.lastName; } }; This is object method in which 'this' refers to the person object. Now 'this' keyword in event handler is used as <button onclick="this.style.display='none'"> So in above 'this' refers to the button element. I hope you''ll understand. Thanks!
29th Apr 2020, 5:21 AM
Shaili Shah
Shaili Shah - avatar
0
I know "this" refers to the owner of method. I should write it. But is this all what "this" can do? Thanks for answer!
29th Apr 2020, 9:04 AM
Алинур Кадыр уулу
Алинур Кадыр уулу - avatar