Javascript - this in Classes
Code Life
By Pierre Marsaa
this Is the Instance Being Used
Imagine two Codémon built from the same class, each calling the same describe() method. The method's code is written only once, yet it prints two different names. The keyword that makes this possible is this:
↗️
Loading editor...
↶
↷
Inside a method, this always means the instance the method was called on — in other words, the object written before the dot:
1.
In zap.describe(), this is zap, so this.name is "Zap".
2.
In aquachu.describe(), this is aquachu, so this.name is "Aquachu".
Same code, different object, different result.
Verify