← ACARA Digital Technologies Years 9–10

The this keyword in a class

Inside a class, this means the object being worked on, so this.name is that object's own name property. Leaving out this causes a ReferenceError.

Understanding The this keyword in a class in ACARA Digital Technologies Years 9–10

What it does: this.score attaches the score to the particular object being built, so each object has its own score. Inside greet(), this.name looks up the name of the object that greet() was called on. Properties are not plain variables. You need this.score to reach the property. Plain score is only a variable name, and none exists. The property must be on the left of =, so the value is saved on the object.

Common mistakes: A constructor parameter must be a plain name like name. this.name belongs inside the constructor body, where it stores the value on the object. It should be constructor(name). == compares two values, so nothing is stored. To give the object its own name property you need the assignment operator: this.name = name;.

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 728–742) and word search (Modules 710–742).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Inside a constructor, which line stores the name parameter on the new object?
name = this.name;this.name = name;
Matches
constructor(name) { name = this.name; }
↓
The assignment is backwards. It copies the empty this.name into the parameter instead of storing the parameter on the object. It should be this.name = name;
Applying
Meanings
Term
this.name
Meaning
Inside a class, this means the object being worked on, so this.name is that object's own name property. Leaving out this causes a ReferenceError.
Practice
class Pet {
Testing
Gaps
.name = name;
Check Answer
Spots
1class Pet {
2 constructor(this.name) {

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

The 'Arctic Hare' badge — Speed Indexer, 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. Inside a constructor, which line stores the name parameter on the new object?
A. this.name = name; ✓
B. name = this.name;
this.name is the new object's own property. The value goes into it with this.name = name;. The other line copies the property over the parameter, which is backwards.
2. What does this refer to inside a class method?
A. The class itself, shared by every object
B. The object the method was called on ✓
If you call dog1.speak(), this is dog1. Each object has its own this, so each one uses its own property values.
3. Which method correctly reads the object's own name?
A. speak() { console.log(name); }
B. speak() { console.log(this.name); } ✓
Without this, JavaScript looks for a plain variable called name. It can't find one, so you get a ReferenceError.
4. A method uses console.log(score) instead of console.log(this.score). What happens?
A. ReferenceError: score is not defined ✓
B. It prints the object's score property
Properties are not plain variables. You need this.score to reach the property. Plain score is only a variable name, and none exists.
5. You create a pet with new Pet('Milo') and the constructor runs this.name = name;. What is pet.name?
A. 'Milo' ✓
B. undefined
The constructor's this is the new object. this.name = name; saves 'Milo' as that object's name property.

Match the Pairs

this.name = name; → Inside a constructor, stores the name value on the new object as its own name property
return this.name + ' barks'; → Inside a method, reads the name property of the object the method was called on
Inside a class, this means the object being worked on, so each object keeps its own data → this.score = score;
A method uses this to read one of its own object's properties → greet() { console.log(this.name); }
constructor(name) { name = this.name; } → The assignment is backwards. It copies the empty this.name into the parameter instead of storing the parameter on the object. It should be this.name = name;
describe() { return name; } → Leaving out this means JavaScript looks for a plain variable called name and throws a ReferenceError. It should be return this.name;

Key Term

this.name
Inside a class, this means the object being worked on, so this.name is that object's own name property. Leaving out this causes a ReferenceError.

Fill in the Blanks

1. constructor(name) { this.name = name; }
Hint: Type the keyword that means the object being created by the constructor.
2. speak() { console.log(`I am ${this.name}`); }
Hint: Type the keyword that refers to the object the method is working on.
3. constructor(pet) { this.name = pet; }
Hint: Type the parameter name that holds the value passed in to the constructor.
4. constructor(name) { this.name = name; }
Hint: Type the special method name that sets up each new object's own properties.
5. addPoints() { this.points += 5; }
Hint: Type the operator that adds 5 to this object's existing points value.

Spot the Error

Question 1
A class Pet {
B constructor(this.name) {
C this.name = name;
D }
E }
Line B — A constructor parameter must be a plain name like name. this.name belongs inside the constructor body, where it stores the value on the object. It should be constructor(name).
Question 2
A class Cat {
B constructor(name) {
C this.name == name;
D }
E }
Line C — == compares two values, so nothing is stored. To give the object its own name property you need the assignment operator: this.name = name;
Question 3
A class Hero {
B constructor(name, hp) {
C this.name = name;
D hp = hp;
E }
F }
Line D — Without this, hp = hp only assigns the parameter to itself, so the object never gets an hp property. It should be this.hp = hp;
Question 4
A showScore() {
B console.log(score);
C }
Line B — Inside a method, score on its own is not a variable, so JavaScript throws a ReferenceError. The object's own property needs this: console.log(this.score);
Question 5
A class Player {
B constructor(name, pts) {
C This.name = name;
D this.pts = pts;
E }
F }
Line C — this must be all lowercase. This is not defined, so it causes a ReferenceError. It should be this.name = name;

Student with a class code? Join your class →