What is a class in JavaScript?
A class is a template for creating objects with shared structure and behavior, introduced in ES6 as syntactic sugar over prototypes.
How do you define a class in JavaScript?
Use the class keyword followed by a class name and a block that defines a constructor and methods.
What is the purpose of the constructor() method?
The constructor is a special method used for initializing object properties when creating a new instance with new.
How do you create an instance of a class?
By using the new keyword, e.g., const person = new Person("Alice").
Can JavaScript classes have multiple constructors?
No, a class can have only one constructor. Attempting to define more will result in a syntax error.
How do you define methods inside a class?
Simply define them as function-like declarations inside the class body without the function keyword.
How do you call an instance method?
Use dot notation on an instance, e.g., person.greet().
What is the difference between instance methods and static methods?
Instance methods belong to objects created from the class, while static methods belong to the class itself and are called directly on it.
How do you define a static method in a JavaScript class?
Prefix the method with the static keyword, e.g., static compare(a, b) { ... }.
How do you call a static method?
Call it directly on the class, not an instance, e.g., Person.compare(p1, p2).
How do you create a subclass in JavaScript?
Use the extends keyword to make a class inherit from another class.
What does the extends keyword do?
It sets up the prototype chain so the subclass inherits properties and methods from its parent class.
What is the purpose of super() in a subclass constructor?
It calls the parent class’s constructor, allowing access to the parent’s properties and initialization logic.
When must super() be called in a subclass?
If you define a constructor in a subclass, you must call super() before using this.
Can a subclass override methods from the parent class?
Yes, by defining a method with the same name, the subclass can override the parent’s method.
What are public class fields?
They are properties declared directly in the class body (outside of the constructor), automatically added to each instance.
What are private class fields in JavaScript?
Private fields are declared with a # prefix and can only be accessed inside the class that defines them.
Why use private fields?
They enforce encapsulation, hiding implementation details from outside access.
Can private fields be inherited or accessed by subclasses?
They are not accessible from subclasses or outside code; each class defines its own private fields.
What is a getter in a JavaScript class?
A getter is a method defined with the get keyword that allows you to access a computed property as if it were a simple field.
What is a setter in a JavaScript class?
A setter is a method defined with the set keyword that lets you control how a property’s value is assigned.
How are getters and setters used in classes?
They provide controlled access to internal data, e.g., get name() { return this._name; } and set name(value) { this._name = value; }.
Can classes have computed method names?
Yes. You can use square brackets to define methods with dynamic names, e.g., [methodName]() { ... }.
What is the this keyword in class context?
this refers to the current instance of the class; it’s used to access instance properties and methods.