Classes & Inheritance
Difficulty: hard
Overview
ES6 classes are syntactic sugar over prototype-based inheritance. A class body contains a constructor and methods. extends creates a subclass; super() must be called in the subclass constructor before accessing this. Static methods belong to the class, not instances. Private fields (#field) are only accessible inside the class body. instanceof checks the prototype chain.
Practice Linked Questions
Q1. What is the correct way to define a class in ES6?
Select one answer before revealing.
Q2. What is the output? ```js class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a noise.`; } } class Dog extends Animal { speak() { return `${this.name} barks.`; } } const d = new Dog("Rex"); console.log(d.speak()); ```
Select one answer before revealing.
Q3. What does the `static` keyword do in a class?
Select one answer before revealing.
Q4. What are private class fields in JavaScript?
Select one answer before revealing.
Q5. What is the output? ```js class Counter { #count = 0; increment() { this.#count++; } get value() { return this.#count; } } const c = new Counter(); c.increment(); c.increment(); console.log(c.value); ```
Select one answer before revealing.