← ACARA Digital Technologies Years 9–10

Common bugs in classes and objects

Common class bugs: forgetting new, leaving out this. in the constructor, swapping the arguments, or misspelling a property so it reads undefined.

Understanding Common bugs in classes and objects in ACARA Digital Technologies Years 9–10

What it does: this.name is the object's property and name is the parameter. Without this., the value is lost when the constructor finishes. Name comes first and age second. Writing new Pet(3, 'Rex') would swap the values and give the wrong properties. Calling a class without new throws: Class constructor Pet cannot be invoked without 'new'. Arguments are matched by position. Swapping them puts 3 in name and 'Rex' in age. nmae is a misspelt property name. JavaScript does not throw an error, it just gives undefined. The error message tells you the fix: add new before the class name. A class must be called with new. The fix is const p = new Pet('Rex', 3);.

Common mistakes: 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"). 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 743–781) and word search (Modules 743–809).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly creates a Pet object from a class?
const p = Pet('Rex', 3);const p = new Pet('Rex', 3);
Matches
const p = Pet('Rex', 3);
↓
The word new is missing, so JavaScript throws the error: Class constructor Pet cannot be invoked without 'new'.
Applying
Meanings
Term
new Pet(name, age)
Meaning
Common class bugs: forgetting new, leaving out this. in the constructor, swapping the arguments, or misspelling a property so it reads undefined.
Practice
class Pet {
Testing
Gaps
const pet = Pet("Rex", 3);
Check Answer
Spots
3}
4const p = Pet("Rex");

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Cosmic Dragon' badge — Orbit Master, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly creates a Pet object from a class?
A. const p = Pet('Rex', 3);
B. const p = new Pet('Rex', 3); ✓
Calling a class without new throws: Class constructor Pet cannot be invoked without 'new'.
2. Which constructor correctly saves the name on the object?
A. constructor(name) { this.name = name; } ✓
B. constructor(name) { name = name; }
Without this., the value is never stored on the object. name = name just assigns the parameter to itself.
3. The class has constructor(name, age). Which call gives the right values?
A. new Pet('Rex', 3) ✓
B. new Pet(3, 'Rex')
Arguments are matched by position. Swapping them puts 3 in name and 'Rex' in age.
4. The class sets this.name. What does console.log(p.nmae) print?
A. Rex
B. undefined ✓
nmae is a misspelt property name. JavaScript does not throw an error, it just gives 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
The error message tells you the fix: add new before the class name.

Match the Pairs

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
console.log(p.nme); → Prints undefined, because the property is called name and nme is a typo
Inside a constructor, this. saves a parameter as a property on the new object → this.name = name;
Arguments must be passed in the same order as the constructor parameters, for example constructor(name, age) → new Pet('Rex', 3)
const p = Pet('Rex', 3); → The word new is missing, so JavaScript throws the error: Class constructor Pet cannot be invoked without 'new'.
constructor(name) { name = name; } → The this. is missing, so the value is never stored on the object and p.name will be undefined.

Key Term

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.

Fill in the Blanks

1. const pet = new Pet("Rex", 3);
Hint: Type the keyword that must come before a class name to create an object, or JavaScript throws an error.
2. class Pet { constructor(name) { this.name = name; } }
Hint: Type the keyword that refers to the object being built, so the value is stored as a property.
3. // Pet constructor(name, age) const n = "Rex", a = 3; const p = new Pet(n, a);
Hint: Type the variable that matches the first constructor parameter, so the arguments are in the right order.
4. // Pet sets this.name and this.age const pet = new Pet("Rex", 3); console.log(pet.name);
Hint: Type the property name spelt exactly as the constructor sets it, to avoid undefined.
5. constructor(name, age) { this.name = name; this.age = age;
Hint: Type the constructor parameter that holds the value for this property, not a different name.

Spot the Error

Question 1
A class Pet {
B constructor(n) {this.n = n;}
C }
D const p = Pet("Rex");
E console.log(p.n);
Line 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").
Question 2
A const f = () => Dog("Bo");
B class Dog {
C constructor(name) {
D this.name = name;
E }
F }
Line 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.
Question 3
A class Car {
B construtor(make, year) {
C this.make = make;
D this.year = year;
E }
F }
Line 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.
Question 4
A class Game {
B constructor(title) {
C this.title = title;
D this.level = level;
E }
F }
Line B — The constructor is missing the level parameter, so this.level = level has no level to read. It should be constructor(title, level).
Question 5
A class Fish {
B constructor(name) {
C this.nmae = name;
D }
E show() { return this.name; }
F }
Line C — The property name is misspelt. This creates this.nmae, so reading this.name later gives undefined. It should be this.name = name.

Student with a class code? Join your class →