1. Classes in TypeScript Flashcards

1
Q

What value do classes bring over having objects without classes?

A

It ensures consistency across the objects, as well as ensuring that certain objects will have certain properties.

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

How does a class relate to a prototype?

A

Classes are syntactic sugar around prototypes.

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

How do you assign a type to a property in TypeScript?

A

By using columns to specify its type.

eg. title : string;

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

How do you define a constructor for a class in TypeScript?

A

By using the constructor keyword as if you were defining a method.

eg.

constructor(title:string, message:string) {
this.title = title;
this.message = message;
}
~~~
~~~

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

What are methods?

A

Functions that belong to our classes.

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

How do you define the return type of a method in TypeScript?

A

By using semicolons, after which you write the return type.

eg. doSomething() : string { //…

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

What is a getter in TypeScript?

A

A property that does some logic to return a value.

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

How do you define a getter in TypeScript?

A

By using the get keyword and defining the rest like a function.

eg. get messageStatus() : string { //…

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

How do you call a getter of a class in TypeScript?

A

As if you were accessing one of its properties.

eg. message.messageStatus;

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

What is a setter in TypeScript?

A

A property that updates data while providing some additional logic.

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

How do you define a setter in TypeScript?

A

By using a setter and defining the rest like a function that takes in a value and assigns it to a private property.

eg.

private _isSent : boolean;
set isSent(value:boolean) {
this._isSent = value;
}
~~~
~~~

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

How do you use a setter in TypeScript?

A

By using an assignment operator to pass in the value it takes.

eg. message.isSent = true;

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