JavaScript Derived Constructor Initialization Order
Quick answer
A derived constructor normally obtains its this binding from super(). Its instance fields initialize after the base constructor returns and before the next statement after super.
How it works
Base instance fields initialize before the base constructor body. A derived constructor cannot read this before super initializes it. Calling super twice fails. A derived constructor may explicitly return an object without super, but this bypasses normal derived field initialization; do not use it as a routine pattern. A non-object non-undefined return from a derived constructor throws.
const log=[]; class Base { a = log.push("base field"); constructor(){log.push("base body");} }class Child extends Base { b = log.push("child field"); constructor(){log.push("before");super();log.push("after");} }new Child(); console.log(log.join("|"));Expected output in Node.js:
before|base field|base body|child field|afterThe derived constructor begins before super, then base and derived fields initialize in order.
class Base { constructor(){this.n=3;} } class Child extends Base { doubled=this.n*2; constructor(){super();this.n=7;} }const x = new Child(); console.log(x.n,x.doubled);Expected output in Node.js:
7 6The field initializer sees the base value before the later derived-body assignment.
Common mistakes
A base constructor should not assume that overriding derived methods can already read initialized derived fields.
Practice
Trace a base constructor calling an overridden method that reads a derived field; explain the risk.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Derived Constructor Initialization Order — Knowledge Check
Apply the taught concept and predict the output.
When do ordinary base instance fields initialize relative to the base constructor body?
When do derived instance fields initialize in a normal super call?
What happens if a derived constructor accesses this before super?
What is the exact console output in JavaScript Derived Constructor Initialization Order example 1?
const log=[]; class Base { a = log.push("base field"); constructor(){log.push("base body");} }class Child extends Base { b = log.push("child field"); constructor(){log.push("before");super();log.push("after");} }new Child(); console.log(log.join("|"));What is the exact console output in JavaScript Derived Constructor Initialization Order example 2?
class Base { constructor(){this.n=3;} } class Child extends Base { doubled=this.n*2; constructor(){super();this.n=7;} }const x = new Child(); console.log(x.n,x.doubled);