To override a getter in a subclass in JavaScript, you can define a new getter with the same name on the subclass. This will shadow the getter on the parent class and will be used instead when accessing the property on instances of the subclass.
Here's a simple example:
index.tsx260 chars18 lines
In this example, the Parent
class defines a getter for the prop
property that returns the string "parent". The Child
class extends Parent
and defines its own getter for prop
that returns the string "child". When we create instances of Parent
and Child
and access the prop
property, we get the expected values. The getter on the subclass overrides the one on the parent class.
Note that getters are accessed using the same syntax as object properties, so you don't need to call them explicitly like methods. Also, the super
keyword can be used inside the getter of the subclass to access the parent class's implementation of the getter, if needed.
gistlibby LogSnag