A derived class (or subclass) in JavaScript is a class that inherits properties and methods from another class (the base or superclass) using theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
extends keyword. It establishes a prototype chain connection between the child class and the parent class, allowing the derived class to inherit, override, or augment the parent’s behavior.
Syntax
Technical Mechanics
1. The extends Keyword and Prototype Chain
When a derived class is created using extends, JavaScript establishes two distinct [[Prototype]] linkages:
- Instance Prototype:
DerivedClass.prototype.[[Prototype]]is set toBaseClass.prototype. This allows instances of the derived class to inherit instance methods. - Static Prototype:
DerivedClass.[[Prototype]]is set toBaseClass. This allows the derived class to inherit static properties and methods directly from the base class constructor.
2. The super Keyword and this Binding
The super keyword serves two distinct purposes depending on its execution context within the derived class:
- As a Constructor (
super(...)): In a derived class, the parent constructor is responsible for allocating and initializing thethisbinding. Therefore, if a derived class defines aconstructor, it must callsuper()before attempting to access or mutatethis. - As an Object (
super.method()): Inside methods (both static and instance),superacts as a reference to the parent class’s prototype object (or the parent class itself, in static contexts), allowing the derived class to invoke overridden parent methods.
3. The Object Return Exception
While omittingsuper() in a derived constructor typically results in a ReferenceError upon returning, there is a critical exception: if the derived constructor explicitly returns an object, calling super() is not required.
Returning an object (such as a Proxy wrapping the instance, or a completely distinct object) bypasses the need to initialize this. Because the explicitly returned object replaces the default instance, the JavaScript engine does not throw a ReferenceError for the uninitialized this binding.
4. Implicit Constructors
If a derived class does not explicitly define aconstructor, the JavaScript engine automatically injects a default constructor that passes all arguments to the parent class:
Execution Examples
Standard Initialization:super() via Object Return:
Field Initialization Order
In a standard derived class instantiation, the initialization sequence strictly follows this order:- The derived constructor is called.
super()is executed, invoking the base class constructor.- Base class fields are initialized.
- The base class constructor body executes.
- Control returns to the derived class.
- Derived class fields are initialized.
- The remainder of the derived class constructor body executes.
Master JavaScript with Deep Grasping Methodology!Learn More





