Typescript Classes 10 exercises
Problem

Implement Class Methods in TypeScript

In this exercise, we've simplified our CanvasNode class so that it no longer has read-only properties:


class CanvasNode {
x = 0;
y = 0;
}

There is a new test case for being able to move the CanvasNode object to a new location:


it("Should be able to move to

Loading exercise

Transcript

00:00 In this exercise, we've slightly simplified our class CanvasNode declaration. We've said we don't need to worry about read-only-ness anymore, so we've just said x equals 0 and y equals 0. Look how beautiful that is. Very, very nice. And now in our tests, we have const CanvasNode equals new CanvasNode,

00:17 and it should be working that CanvasNode.x starts as 0, CanvasNode.y starts as 0. But we now should be having a method on CanvasNode which says move. And move should move it to that location. So you pass in an x-coordinate and a y-coordinate,

00:36 and then CanvasNode.x should then equal 10 and 20, which is the coordinates that we're passing in. Your job is to try to figure out a way to add that method to this class, passing in x and y, and making sure that it ends up with the right coordinates in place. Good luck.