Classes in JavaScript Flashcards

(33 cards)

1
Q

What is a class in JavaScript?

A

A class is a template for creating objects with shared structure and behavior, introduced in ES6 as syntactic sugar over prototypes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you define a class in JavaScript?

A

Use the class keyword followed by a class name and a block that defines a constructor and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of the constructor() method?

A

The constructor is a special method used for initializing object properties when creating a new instance with new.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create an instance of a class?

A

By using the new keyword, e.g., const person = new Person("Alice").

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can JavaScript classes have multiple constructors?

A

No, a class can have only one constructor. Attempting to define more will result in a syntax error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you define methods inside a class?

A

Simply define them as function-like declarations inside the class body without the function keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you call an instance method?

A

Use dot notation on an instance, e.g., person.greet().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between instance methods and static methods?

A

Instance methods belong to objects created from the class, while static methods belong to the class itself and are called directly on it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you define a static method in a JavaScript class?

A

Prefix the method with the static keyword, e.g., static compare(a, b) { ... }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you call a static method?

A

Call it directly on the class, not an instance, e.g., Person.compare(p1, p2).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create a subclass in JavaScript?

A

Use the extends keyword to make a class inherit from another class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the extends keyword do?

A

It sets up the prototype chain so the subclass inherits properties and methods from its parent class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of super() in a subclass constructor?

A

It calls the parent class’s constructor, allowing access to the parent’s properties and initialization logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When must super() be called in a subclass?

A

If you define a constructor in a subclass, you must call super() before using this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can a subclass override methods from the parent class?

A

Yes, by defining a method with the same name, the subclass can override the parent’s method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are public class fields?

A

They are properties declared directly in the class body (outside of the constructor), automatically added to each instance.

17
Q

What are private class fields in JavaScript?

A

Private fields are declared with a # prefix and can only be accessed inside the class that defines them.

18
Q

Why use private fields?

A

They enforce encapsulation, hiding implementation details from outside access.

19
Q

Can private fields be inherited or accessed by subclasses?

A

They are not accessible from subclasses or outside code; each class defines its own private fields.

20
Q

What is a getter in a JavaScript class?

A

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.

21
Q

What is a setter in a JavaScript class?

A

A setter is a method defined with the set keyword that lets you control how a property’s value is assigned.

22
Q

How are getters and setters used in classes?

A

They provide controlled access to internal data, e.g., get name() { return this._name; } and set name(value) { this._name = value; }.

23
Q

Can classes have computed method names?

A

Yes. You can use square brackets to define methods with dynamic names, e.g., [methodName]() { ... }.

24
Q

What is the this keyword in class context?

A

this refers to the current instance of the class; it’s used to access instance properties and methods.

25
Can you define classes as expressions?
Yes, classes can be defined using expressions and assigned to variables, e.g., `const MyClass = class { ... }`.
26
What are anonymous classes?
Classes without a name, often used as expressions or returned from functions.
27
What is a named class expression?
A class expression with a name, which is only visible within the class body itself.
28
How are classes related to prototypes in JavaScript?
Classes are syntactic sugar for prototype-based inheritance. Under the hood, class methods are stored on the prototype object.
29
How can you verify that a method is on a class prototype?
Use `Object.getPrototypeOf(instance)` or `instance.__proto__` to check that class methods are inherited from the prototype.
30
Are class declarations hoisted?
No, unlike function declarations, class declarations are not hoisted; they must be defined before use.
31
What is the difference between a constructor function and a class?
Classes provide a clearer syntax for defining constructor functions and prototype methods, but both achieve the same prototype-based inheritance.
32
Why is using `class` syntax preferred over prototypes?
It provides cleaner, more readable code and enforces better organization of methods and inheritance structure.
33
Can you mix class and prototype syntax?
Yes, but it’s best practice to stick with one approach (preferably `class`) for consistency.