Common bugs in classes and objects

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

Common bugs in classes and objects

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly creates a Pet object from a class?
A const p = Pet('Rex', 3);
B const p = new Pet('Rex', 3);
2. Which constructor correctly saves the name on the object?
A constructor(name) { this.name = name; }
B constructor(name) { name = name; }
3. The class has constructor(name, age). Which call gives the right values?
A new Pet('Rex', 3)
B new Pet(3, 'Rex')
4. The class sets this.name. What does console.log(p.nmae) print?
A Rex
B undefined
5. Which is the cause of this error: Class constructor Pet cannot be invoked without 'new'?
A Calling Pet('Rex', 3) without new
B Using the wrong property name

Match the Pairs

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

Left columnRight column
1const p = new Pet('Rex', 3); ACreates a Pet object with new, passing the name first and the age second, in the same order as the constructor
2console.log(p.nme); Bthis.name = name;
3Inside a constructor, this. saves a parameter as a property on the new object CPrints undefined, because the property is called name and nme is a typo
4Arguments must be passed in the same order as the constructor parameters, for example constructor(name, age) Dnew Pet('Rex', 3)
5const p = Pet('Rex', 3); EThe word new is missing, so JavaScript throws the error: Class constructor Pet cannot be invoked without 'new'.
6constructor(name) { name = name; } FThe this. is missing, so the value is never stored on the object and p.name will be undefined.

Definitions

new Pet(name, age)

Fill in the Blanks

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

1.
const pet = ___ Pet("Rex", 3);
2.
class Pet { constructor(name) { ____.name = name; } }
3.
// Pet constructor(name, age) const n = "Rex", a = 3; const p = new Pet(_, a);
4.
// Pet sets this.name and this.age const pet = new Pet("Rex", 3); console.log(pet.____);
5.
constructor(name, age) { this.name = name; this.age = ___;

Spot the Error

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

Question 1
A class Pet {
B constructor(n) {this.n = n;}
C }
D const p = Pet("Rex");
E console.log(p.n);
Question 2
A const f = () => Dog("Bo");
B class Dog {
C constructor(name) {
D this.name = name;
E }
F }
Question 3
A class Car {
B construtor(make, year) {
C this.make = make;
D this.year = year;
E }
F }
Question 4
A class Game {
B constructor(title) {
C this.title = title;
D this.level = level;
E }
F }
Question 5
A class Fish {
B constructor(name) {
C this.nmae = name;
D }
E show() { return this.name; }
F }

Answer Key

Common bugs in classes and objects
Section A: Quick Questions
Q1
B — const p = new Pet('Rex', 3);
Q2
A — constructor(name) { this.name = name; }
Q3
A — new Pet('Rex', 3)
Q4
B — undefined
Q5
A — Calling Pet('Rex', 3) without new
Section B: Match the Pairs
Q1
const p = new Pet('Rex', 3); → Creates a Pet object with new, passing the name first and the age second, in the same order as the constructor
Q2
console.log(p.nme); → Prints undefined, because the property is called name and nme is a typo
Q3
Inside a constructor, this. saves a parameter as a property on the new object → this.name = name;
Q4
Arguments must be passed in the same order as the constructor parameters, for example constructor(name, age) → new Pet('Rex', 3)
Q5
const p = Pet('Rex', 3); → The word new is missing, so JavaScript throws the error: Class constructor Pet cannot be invoked without 'new'.
Q6
constructor(name) { name = name; } → The this. is missing, so the value is never stored on the object and p.name will be undefined.
Section C: Definitions
Q1
new Pet(name, age) — Common class bugs: forgetting new, leaving out this. in the constructor, swapping the arguments, or misspelling a property so it reads undefined.
Section D: Fill in the Blanks
Q1
const pet = new Pet("Rex", 3);
Q2
class Pet { constructor(name) { this.name = name; } }
Q3
// Pet constructor(name, age) const n = "Rex", a = 3; const p = new Pet(n, a);
Q4
// Pet sets this.name and this.age const pet = new Pet("Rex", 3); console.log(pet.name);
Q5
constructor(name, age) { this.name = name; this.age = age;
Section E: Spot the Error
Q1
D — A class constructor cannot be called without new. Pet("Rex") throws "Class constructor Pet cannot be invoked without 'new'". It should be new Pet("Rex").
Q2
A — The word new is missing. Calling Dog("Bo") like a normal function throws an error. It should be new Dog("Bo") so a new object is created.
Q3
B — The method name is misspelt. Only a method named exactly constructor runs when you use new, so make and year would never be set and would read as undefined. It should be constructor.
Q4
B — The constructor is missing the level parameter, so this.level = level has no level to read. It should be constructor(title, level).
Q5
C — The property name is misspelt. This creates this.nmae, so reading this.name later gives undefined. It should be this.name = name.