Class Constructors — JS Technical Interview Questions & Answers

Initializing instance state with constructor().

Class Constructors

  • Class Constructors (js) is a JS topic. In plain words you use it for let total = 0 in a cart script. Don’t start with a slogan — start with that picture.
  • Smallest example: let for change, const for fixed. Type it, run it, and say what you see. If you can do that from memory, you know Class Constructors (js).
  • From the example next to this theory: Initializing instance state with constructor().
  • Trap: var leak / const reassign. Fix that before you talk about advanced JS.
  • Viva: what is Class Constructors (js)? Then show let for change, const for fixed. Then name the trap.

Technical Interview Tip: What is Class Constructors (js)? Show this: let for change, const for fixed. Trap: var leak / const reassign.

Code Examples for Class Constructors

Class Constructors — working example

class Animal {
  static kingdom = "Animalia";
  #secret = "private";
  constructor(name) { this.name = name; }
  speak() { return this.name + " speaks"; }
  reveal() { return this.#secret; }
}
class Dog extends Animal {
  speak() { return super.speak() + " (bark)"; }
}
const d = new Dog("Rex");
console.log(d.speak(), Animal.kingdom, d.reveal());
console.log(Object.getPrototypeOf(d) === Dog.prototype);

Class Constructors — oOP in JS: constructors, prototypes, inheritance, statics, and private fields (#).

How to Explain Class Constructors to an Interviewer

What is Class Constructors (js)?

Used for let total = 0 a cart script Not just the heading

Give one small example of Class Constructors (js).

let for change, const for fixed Initializing instance state with constructor()

What mistake do beginners make with Class Constructors (js)?

var leak / const reassign Show the fix

Where do you use Class Constructors (js)?

a cart script let total = 0