Reading and changing an object's properties

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

This Reading and changing an object's properties worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (JavaScript) journey. Print it as a booklet?

Reading and changing an object's properties

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Dog class: const rex = new Dog('Rex', 3). Which line reads Rex's age?
A console.log(Dog.age);
B console.log(rex.age);
2. Which line changes rex's age to 4?
A rex.age = 4;
B rex.age == 4;
3. rex.age is 3. After rex.age = 4; runs, what does console.log(rex.age) print?
A 4
B 3
4. const rex = new Dog('Rex', 3); const fido = new Dog('Fido', 5); rex.age = 4; What is fido.age?
A 4
B 5
5. Which is the correct way to read the name property of an object called fido?
A fido.name
B fido(name)

Match the Pairs

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

Left columnRight column
1rex.age Aconsole.log(rex.name);
2rex.age = 4; BReads the age property of the object stored in rex
3Print the value of a property stored in an object called rex CBrackets are used instead of a dot. It should be rex.age to read the property.
4Changing one object's property does not change another object's copy DChanges rex's age property to the new value 4
5rex.age == 4; E== compares values instead of assigning. It should be rex.age = 4; to change the property.
6console.log(rex(age)); Frex.age = 4; console.log(milo.age);

Definitions

object.property

Fill in the Blanks

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

1.
const rex = new Dog("Rex", 3); console.log(___.age);
2.
const rex = new Dog("Rex", 3); rex.age _ 4; console.log(rex.age);
3.
const rex = new Dog("Rex", 3); const mia = new Dog("Mia", 5); ___.age = 6; // rex.age is still 3
4.
const rex = new Dog("Rex", 3); rex.age = rex.___ + 1; console.log(rex.age); // 4
5.
const rex = new Dog("Rex", 3); rex.___ = 4; // Rex is now 4 years old console.log(rex.age);

Spot the Error

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

Question 1
A const rex = new Dog("Rex", 3);
B console.log(rex.Age);
C rex.age = 4;
Question 2
A const rex = new Dog("Rex", 3);
B console.log(rex.age);
C rex.age == 4;
D console.log(rex.age);
Question 3
A const mia = new Cat("Mia", 2);
B mia.age = age + 1;
C console.log(mia.age);
Question 4
A let rex = Dog("Rex", 3);
B let zed = new Dog("Zed", 5);
C rex.age = 4;
D console.log(zed.age);
Question 5
A let rex = new Dog("Rex", 3);
B let zed = new Dog("Zed", 5);
C rex.age = 4;
D console.log(zed.age());

Answer Key

Reading and changing an object's properties
Section A: Quick Questions
Q1
B — console.log(rex.age);
Q2
A — rex.age = 4;
Q3
A — 4
Q4
B — 5
Q5
A — fido.name
Section B: Match the Pairs
Q1
rex.age → Reads the age property of the object stored in rex
Q2
rex.age = 4; → Changes rex's age property to the new value 4
Q3
Print the value of a property stored in an object called rex → console.log(rex.name);
Q4
Changing one object's property does not change another object's copy → rex.age = 4; console.log(milo.age);
Q5
rex.age == 4; → == compares values instead of assigning. It should be rex.age = 4; to change the property.
Q6
console.log(rex(age)); → Brackets are used instead of a dot. It should be rex.age to read the property.
Section C: Definitions
Q1
object.property — Reads a property of an object, like rex.age. To change it, assign a new value: rex.age = 4. Each object keeps its own values.
Section D: Fill in the Blanks
Q1
const rex = new Dog("Rex", 3); console.log(rex.age);
Q2
const rex = new Dog("Rex", 3); rex.age = 4; console.log(rex.age);
Q3
const rex = new Dog("Rex", 3); const mia = new Dog("Mia", 5); mia.age = 6; // rex.age is still 3
Q4
const rex = new Dog("Rex", 3); rex.age = rex.age + 1; console.log(rex.age); // 4
Q5
const rex = new Dog("Rex", 3); rex.age = 4; // Rex is now 4 years old console.log(rex.age);
Section E: Spot the Error
Q1
B — Property names are case-sensitive. rex.Age does not exist, so it prints undefined. It should be rex.age.
Q2
C — == compares two values and does not change anything. To change the property, use the assignment operator: rex.age = 4;
Q3
B — age on its own is not a variable, so this causes a ReferenceError. Read the property through the object: mia.age + 1.
Q4
A — An object is created from a class with the new keyword. It should be let rex = new Dog("Rex", 3);
Q5
D — age is a property, not a method, so it is read without brackets. zed.age() causes a TypeError. Use zed.age, which prints 5.