One class, many objects

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

One class, many objects

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. const a = new Pet('Rex'); const b = new Pet('Mia'); How many Pet objects have been made?
A Two separate objects
B One object with two names
2. a.name = 'Max'; is run on the Rex object. What happens to b.name (the Mia object)?
A It also becomes 'Max'
B It stays 'Mia'
3. Which line makes a second, separate object from the Pet class?
A const b = a;
B const b = new Pet('Mia');
4. Which statement about methods is true?
A All objects made from the class share the same methods
B Each object needs its own different method written in the class
5. rex.speak() prints 'Rex says woof' and mia.speak() prints 'Mia says woof'. Why are the results different?
A this refers to the object that called the method
B The two objects have different speak() methods

Match the Pairs

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

Left columnRight column
1let a = new Pet('Rex'); let b = new Pet('Mia'); Aa.speak(); b.speak();
2a.name = 'Max'; console.log(b.name); BChanging a's name does not change b's name, so b still prints Mia
3Objects from one class share the same method, but the result can differ because each has its own property values CCreates two separate objects from the same Pet class
4One class can be used to build many objects, each with its own values Db = a does not create a new object. Both variables point to the same object, so changing b also changes a. Use let b = new Pet('Mia').
5let a = new Pet('Rex'); let b = a; b.name = 'Mia'; Elet pets = [new Pet('Rex'), new Pet('Mia')];
6let a = Pet('Rex'); let b = Pet('Mia'); FThe new keyword is missing, so no objects are created from the class. It should be new Pet('Rex') and new Pet('Mia').

Definitions

new Pet()

Fill in the Blanks

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

1.
const a = ___ Pet("Rex", 5);
2.
const p1 = new Pet("Rex"); const p2 = new ___("Mia");
3.
const a = new Pet("Rex", 5); const b = new Pet("Mia", 5); _.energy = 1; // b.energy is still 5
4.
// Pet has a speak() method const b = new Pet("Mia"); console.log(b._____()); // Mia says hi
5.
constructor(name) { ____.name = name; }

Spot the Error

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

Question 1
A const rex = new pet("Rex");
B const mia = new Pet("Mia");
C console.log(rex.name);
Question 2
A class Pet {
B contructor(name, age) {
C this.name = name;
D this.age = age;
E }
F }
Question 3
A class Pet {
B constructor(name) {
C name = name;
D this.energy = 5;
E }
F }
Question 4
A const rex = new Pet("Rex");
B const mia = new Pet("Mia");
C rex.name = "Max";
D console.log(Pet.name);
Question 5
A const p1 = new Pet("Rex");
B const p2 = Pet("Mia");
C p1.eat();
D p2.eat();

Answer Key

One class, many objects
Section A: Quick Questions
Q1
A — Two separate objects
Q2
B — It stays 'Mia'
Q3
B — const b = new Pet('Mia');
Q4
A — All objects made from the class share the same methods
Q5
A — this refers to the object that called the method
Section B: Match the Pairs
Q1
let a = new Pet('Rex'); let b = new Pet('Mia'); → Creates two separate objects from the same Pet class
Q2
a.name = 'Max'; console.log(b.name); → Changing a's name does not change b's name, so b still prints Mia
Q3
Objects from one class share the same method, but the result can differ because each has its own property values → a.speak(); b.speak();
Q4
One class can be used to build many objects, each with its own values → let pets = [new Pet('Rex'), new Pet('Mia')];
Q5
let a = new Pet('Rex'); let b = a; b.name = 'Mia'; → b = a does not create a new object. Both variables point to the same object, so changing b also changes a. Use let b = new Pet('Mia').
Q6
let a = Pet('Rex'); let b = Pet('Mia'); → The new keyword is missing, so no objects are created from the class. It should be new Pet('Rex') and new Pet('Mia').
Section C: Definitions
Q1
new Pet() — Each new Pet() makes a separate object from the same class. Every object has its own property values, but all of them share the same methods. Changing one object does not change the others.
Section D: Fill in the Blanks
Q1
const a = new Pet("Rex", 5);
Q2
const p1 = new Pet("Rex"); const p2 = new Pet("Mia");
Q3
const a = new Pet("Rex", 5); const b = new Pet("Mia", 5); a.energy = 1; // b.energy is still 5
Q4
// Pet has a speak() method const b = new Pet("Mia"); console.log(b.speak()); // Mia says hi
Q5
constructor(name) { this.name = name; }
Section E: Spot the Error
Q1
A — JavaScript is case-sensitive, so new pet() does not match the class Pet. It should be new Pet("Rex").
Q2
B — The method name is misspelled. It must be constructor, otherwise it will not run each time new Pet() makes an object.
Q3
C — Without this, the value is not stored on the new object. It should be this.name = name; so each object keeps its own name.
Q4
D — Pet is the class, not one of the objects. To read Mia's own property value, use the object: mia.name. It is still "Mia" because changing rex does not change mia.
Q5
B — The new keyword is missing. A class must be called with new Pet("Mia") to create a separate object.