Typescript Classes 10 exercises

Getter Methods in TypeScript

Let's continue working with the CanvasNode class. It has a constructor that accepts an optional argument, renamed to position. This position is an object that replaces the individual x and y we had before. The move method is still in place:

class CanvasNode {
x: number;
y: nu

Loading exercise


00:00 In this exercise, our CanvasNode basically remains the same. We still have a kind of optional argument you can pass into the constructor, but I've renamed it to position instead of ops, which it was before. We've still got our move function too. And we've now got a couple of differences in our test cases.

00:17 So instead of just having X and Y here, we now have a object which is called position, which we're expecting to receive on the CanvasNode. So CanvasNode.position is basically supposed to, at all times, equal whatever X and Y is inside an object.

00:35 So all the way down here, we've got our position working. Now you might think, okay, this is a bit annoying. I need to save an extra variable inside here called like position with an object with X and Y. But there is a more interesting way to do it using a getter. So your job is to try to figure out

00:54 how you would use a getter in this situation to represent position. And I will see you on the other side.