Getters & Setters Flashcards

1
Q

Why are getters useful?

A

If we have an object which contains a mutable property, we can use a getter to allow access to that property without risking the user mutating the data unintentionally.

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

How do you define a getter?

A

get students() {
return […this._students]; // A copy of the _students array
},

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

What is the syntax of a setter?

A

a setter looks like a concise syntax method definition preceded by the keywordset

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

Why can there be a risk of stack overflow with getter?

A

if the getter has the same name as the thing you are trying to get, you can end up in a recursive loop as the getter ends up calling itself each time it runs.

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

What do setters take?

A

A setter takes a single argument: the value you want to assign to the property.

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

How do you use getters and setters?

A

you use setters & getters just like ordinary properties, not methods. In other words, you writestudent.firstName = something,notstudent.firstName(something). or just student.firstName

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