Modifying an existing class

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Modifying an existing class

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You add a new parameter to a class constructor. What else must you update?
A Nothing, old new calls keep working automatically
B Every new call that creates objects from the class
2. Which constructor correctly adds a score property to a Player class?
A constructor(name, score) { this.name = name; this.score = score; }
B constructor(name, score) { this.name = name; score = score; }
3. A Player class now has constructor(name, score). Which call matches it?
A new Player('Ava', 10)
B new Player('Ava')
4. You add a new method to a class. Where does it go?
A Inside the constructor, after the this lines
B Inside the class braces, next to the constructor
5. You changed a class. What should you do before you trust it again?
A Re-run your tests and check they still pass
B Skip the tests because the change was small

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1constructor(name, score) { AThe constructor was changed to need two values, but this new call was not updated, so score is undefined.
2this.score = score; BaddPoints(n) { this.score += n; }
3After adding a score parameter to the constructor, update the code that creates objects so it passes the new value. CThe constructor now takes a new parameter, score, so every new call must be updated to pass it in.
4Adding a new method to a class means writing it inside the class body, then calling it on an object. DAdds a new property to the class by saving the score parameter on each object that is created.
5constructor(name, score) { this.name = name; } let p = new Player('Ava', 10); EThe score parameter was added but never stored with this.score = score, so the object has no score property.
6constructor(name, score) { ... } let p = new Player('Ava'); Flet p1 = new Player('Ava', 10);

Definitions

this.score = score;

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
constructor(name, _____) { this.name = name; this.score = score;
2.
constructor(name, score) { this.name = name; this._____ = score;
3.
addPoints(n) { this.score __ n; }
4.
const p1 = ___ Player("Ava", 10);
5.
const p = new Player("Ava", 10); p.addPoints(5); console.assert(p.score ___ 15);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Player {
B constructor(name) {
C this.name = name;
D this.score = score;
E }
F }
Question 2
A class Pet {
B constructor(name, age) {
C this.name = name;
D this.age = name;
E }
F }
Question 3
A class Player {
B addPoints(n) {
C this.score + n;
D }
E }
Question 4
A let a = new Car("Kia");
B let b = new Car("VW", 2018);
C console.log(a.year);
D console.log(b.year);
Question 5
A let p = new Player("Ana", 0);
B p.addPoints(5);
C p.addPoints(10);
D console.assert(p.score = 15);

Answer Key

Modifying an existing class
Section A: Quick Questions
Q1
B — Every new call that creates objects from the class
Q2
A — constructor(name, score) { this.name = name; this.score = score; }
Q3
A — new Player('Ava', 10)
Q4
B — Inside the class braces, next to the constructor
Q5
A — Re-run your tests and check they still pass
Section B: Match the Pairs
Q1
constructor(name, score) { → The constructor now takes a new parameter, score, so every new call must be updated to pass it in.
Q2
this.score = score; → Adds a new property to the class by saving the score parameter on each object that is created.
Q3
After adding a score parameter to the constructor, update the code that creates objects so it passes the new value. → let p1 = new Player('Ava', 10);
Q4
Adding a new method to a class means writing it inside the class body, then calling it on an object. → addPoints(n) { this.score += n; }
Q5
constructor(name, score) { this.name = name; } let p = new Player('Ava', 10); → The score parameter was added but never stored with this.score = score, so the object has no score property.
Q6
constructor(name, score) { ... } let p = new Player('Ava'); → The constructor was changed to need two values, but this new call was not updated, so score is undefined.
Section C: Definitions
Q1
this.score = score; — Modifying a class: add or change a constructor parameter, property or method, then update every new call that creates its objects and re-run your tests.
Section D: Fill in the Blanks
Q1
constructor(name, score) { this.name = name; this.score = score;
Q2
constructor(name, score) { this.name = name; this.score = score;
Q3
addPoints(n) { this.score += n; }
Q4
const p1 = new Player("Ava", 10);
Q5
const p = new Player("Ava", 10); p.addPoints(5); console.assert(p.score === 15);
Section E: Spot the Error
Q1
B — The new score property was added to the constructor body, but the parameter was not added. 'score' is undefined, so the constructor should be constructor(name, score).
Q2
D — The new age property is being given the name parameter. It should store the age parameter: this.age = age;
Q3
C — The new method works out score + n but never saves it. To change the property, use this.score += n;
Q4
A — The constructor now takes a year, so every new call must be updated. This call is missing 2020, so a.year would be undefined.
Q5
D — A single = assigns 15 to score instead of comparing, so the test always passes. Use == or === to compare: p.score == 15.