#this keyword inside Class

1 messages · Page 1 of 1 (latest)

astral pagoda
#

Hello all, I am just wondering why we need to declare the this keyword before the getHealthBar() method, it is obvious that the getHealthBar() method is not inside the constructor function, and if i delete the this keyword from the method it's throwing the error that the getHealthBar() is not defined.. can anybody pls explain this to me?

sturdy thicket
#

Disambiguation. The this keyword is required because that is how to tell JavaScript explicitly that you want to call the getHealthBarHtml() method that is part of the class. Imagine if the this keyword was not required, and you also had a global function named getHealthBarHtml(). How would you be able to tell JavaScript which one of those you wanted to call? By requiring the this before the method name, you are able to tell JavaScript that you want the getHealthBarHtml() class method to be called, not the global function. Does this make sense?

astral pagoda
#

the this keyword is refering to the particuar instance of the class.. it is saying that the method is executing with the particular instance. this is my understanding

sturdy thicket
#

Yes, I think that is an accurate description. In a class method, this is a reference to the specific object/instance that has called the method.