Despite my initial assumptions, I found myself unable to discover a definitive answer through numerous Google searches on this topic.
This question pertains to the use of the class pattern in Ecmascript 6 and beyond.
I initially believed that method overriding
in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. Wikipedia
However, I found that Method Shadowing (which goes beyond just block scope 'variable shadowing'- Wikipedia) only seemed to be relevant in a strongly typed language like C#, where an instance of a 'child class' can be set as type 'base class', causing the instance to revert back to the base class methods instead of any 'shadowed' methods.
public abstract class BaseClass
{
public virtual void shadowedMethod()
{
Console.WriteLine("This is the BaseClass version");
}
}
public class DerivedClass : BaseClass
{
public new void shadowedMethod()
{
Console.WriteLine("This is the Derived child class");
}
}
public class Program
{
public static void Main()
{
BaseClass instance = new DerivedClass(); // Because BaseClass type is set instead of DerivedClass
instance.shadowedMethod(); // It prints "This is the BaseClass version"
}
}
The code was adapted from this article
Therefore, the question arises: why do most JavaScript threads and documentationECMA Standard use override and shadow interchangeably (but tend to favor shadow)? Shouldn't we adopt one term to mitigate confusion? Is there a nuanced difference in JavaScript between overriding and shadowing a method?