10 questions found
What is a class in JavaScript, and is it a new kind of object model?
Beginner
A class is syntactic sugar over JavaScript's existing prototype-based inheritance. It doesn't add a new object model — under the hood, methods still live on the prototype object, just written with cleaner syntax.
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound.`; }
}
const a = new Animal('Rex');
console.log(a.speak());
Real-world example
Modeling domain entities like User, Order, or Product with clear, readable structure.
Common follow-ups: Is a class hoisted the same way a function declaration is?
Prototypes & Inheritance
How does the constructor method work in a class?
Beginner
constructor() runs automatically when you call new ClassName(...), initializing instance properties. A class can have at most one constructor.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
const p = new Point(3, 4);
Real-world example
Setting up a new object's initial state, like a shopping cart starting with an empty items array.
Common follow-ups: What happens if you don't define a constructor at all?
this & Binding
How do you create a subclass using extends?
Beginner
extends sets up prototype-based inheritance between classes, and super() inside the subclass's constructor calls the parent class's constructor to initialize inherited state before adding subclass-specific properties.
class Animal {
constructor(name) { this.name = name; }
}
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
}
Real-world example
Modeling an AdminUser class that extends a base User class with extra permissions.
Common follow-ups: Must super() be called before using 'this' in a subclass constructor?
Prototypes & Inheritance
What are class fields, and how do they differ from constructor-assigned properties?
Intermediate
Class fields let you declare and initialize instance properties directly in the class body, outside the constructor, which runs before the constructor body executes. They reduce boilerplate especially with inheritance and default values.
class Counter {
count = 0; // class field
increment() { this.count++; }
}
Real-world example
Declaring default component state fields cleanly at the top of a class, without cluttering the constructor.
Common follow-ups: Are class fields defined before or after super() runs in a subclass?
Objects
Property Descriptors & Immutability
What does the # syntax mean for class members?
Intermediate
The # prefix marks a truly private field or method, accessible only from inside the class body itself — not even subclasses or external code can read or call it, and accessing it from outside throws a SyntaxError, not just returning undefined.
class BankAccount {
#balance = 0;
deposit(amount) { this.#balance += amount; }
getBalance() { return this.#balance; }
}
const acc = new BankAccount();
// acc.#balance; // SyntaxError outside the class
Real-world example
Hiding an account's internal balance so it can only be modified through controlled methods like deposit()/withdraw().
Common follow-ups: How did developers fake privacy before # private fields existed?
Design Patterns in JavaScript
What are static methods and properties, and when do you use them?
Intermediate
static members belong to the class itself, not to instances — you call them as ClassName.method() rather than instance.method(). They're useful for factory methods, utility functions, or shared counters tied to the class as a whole.
class User {
static count = 0;
constructor() { User.count++; }
static fromJSON(json) { return new User(JSON.parse(json)); }
}
Real-world example
A static User.fromJSON() factory method that builds a User instance from an API response.
Common follow-ups: Can static methods access instance properties directly?
JSON & Data Serialization
How do getters and setters work inside a class?
Advanced
get and set define accessor properties that look like plain fields from the outside but run custom logic on read or write — useful for validation or computed values without changing how consumers interact with the object.
class Temperature {
#celsius = 0;
get fahrenheit() { return this.#celsius * 9/5 + 32; }
set fahrenheit(f) { this.#celsius = (f - 32) * 5/9; }
}
const t = new Temperature();
t.fahrenheit = 98.6;
Real-world example
Exposing a computed fullName getter derived from firstName and lastName fields.
Common follow-ups: Do getters/setters count as own properties when you use Object.keys()?
Objects
Property Descriptors & Immutability
How does method overriding with super work in JavaScript classes?
Advanced
A subclass can redefine a parent's method by declaring one with the same name; calling super.methodName() from inside the override lets you still invoke the parent's original implementation, typically to extend rather than fully replace it.
class Shape {
describe() { return 'A shape'; }
}
class Circle extends Shape {
describe() { return super.describe() + ', specifically a circle'; }
}
new Circle().describe(); // 'A shape, specifically a circle'
Real-world example
Extending a base Logger class's log() method to add a timestamp prefix while still writing the original message.
Common follow-ups: Can you call super outside of a class method?
Design Patterns in JavaScript
What is a static initialization block and why would you use one?
Advanced
A static {} block runs once when the class is defined, letting you run multi-step setup logic for static fields that a simple assignment can't express — including try/catch around initialization.
class Config {
static settings;
static {
try {
Config.settings = JSON.parse(loadConfigString());
} catch {
Config.settings = {};
}
}
}
Real-world example
Loading and validating shared configuration once when a class module is first imported.
Common follow-ups: Can a class have multiple static blocks?
Error Handling
Why can classes not be called without 'new', unlike regular functions?
Advanced
Class constructors are designed to enforce proper object construction; calling a class without new throws a TypeError by specification, unlike ordinary functions which silently run with 'this' bound to undefined (in strict mode) or the global object.
class Foo {}
Foo(); // TypeError: Class constructor Foo cannot be invoked without 'new'
Real-world example
Preventing a common bug where a class is accidentally called like a plain function and silently misbehaves.
Common follow-ups: How do factory functions differ from classes in this respect?
this & Binding