A method inside an object literal

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

A method inside an object literal

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which object literal correctly defines a method called greet?
A const person = { greet() { console.log('Hi'); } };
B const person = { greet { console.log('Hi'); } };
2. Which line correctly calls the greet method on the person object?
A person.greet;
B person.greet();
3. What is a method?
A A variable that stores only numbers
B A function stored as a property of an object
4. Which call uses the dot correctly to run a method?
A dog.bark()
B bark.dog()
5. How is a method different from a plain function?
A A method belongs to an object and is called with object.method()
B A method is called by writing just its name, like greet()

Match the Pairs

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

Left columnRight column
1const dog = { speak() { console.log('Woof!'); } }; Aconst player = { jump() { return 'Up!'; } };
2dog.speak(); BCalls the speak method on the dog object
3A method is a function stored as a property of an object, so it is written inside the object's curly braces CCreates an object literal with a method called speak stored inside it
4A plain function is called by its name alone, but a method must be called with the object name, a dot and brackets DThe brackets are missing, so the method is not called. It only refers to the function and nothing runs. Fix: cat.meow();
5const cat = { meow() { console.log('Meow'); } }; cat.meow; Eplayer.jump();
6const cat = { meow() { console.log('Meow'); } }; meow(); FThe method is called like a plain function. It belongs to cat, so it needs the dot: cat.meow();

Definitions

greet() { ... }

Fill in the Blanks

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

1.
const pet = { speak() { return "Woof"; } }; console.log(pet_speak());
2.
const pet = { _____() { return "Woof"; } }; console.log(pet.speak());
3.
const user = { name: "Mia", hello() { return "Hi " + ____.name; } };
4.
const maths = { triple(n) { ______ n * 3; } };
5.
const calc = { double(n) { return n * 2; } }; console.log(calc.______(4));

Spot the Error

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

Question 1
A const dog = {
B name: "Rex",
C bark { return "Woof"; }
D };
E console.log(dog.bark());
Question 2
A const cat = {
B name: "Mia",
C word: "Meow",
D say() { return word; }
E };
Question 3
A const car = {
B go() { return "Vroom"; }
C };
D console.log(car.go);
Question 4
A const user = {
B name: "Sam"
C hello() { return "Hi"; }
D };
E console.log(user.hello());
Question 5
A const pet = [
B name: "Bo",
C speak() { return "Hi"; }
D };
E console.log(pet.speak());

Answer Key

A method inside an object literal
Section A: Quick Questions
Q1
A — const person = { greet() { console.log('Hi'); } };
Q2
B — person.greet();
Q3
B — A function stored as a property of an object
Q4
A — dog.bark()
Q5
A — A method belongs to an object and is called with object.method()
Section B: Match the Pairs
Q1
const dog = { speak() { console.log('Woof!'); } }; → Creates an object literal with a method called speak stored inside it
Q2
dog.speak(); → Calls the speak method on the dog object
Q3
A method is a function stored as a property of an object, so it is written inside the object's curly braces → const player = { jump() { return 'Up!'; } };
Q4
A plain function is called by its name alone, but a method must be called with the object name, a dot and brackets → player.jump();
Q5
const cat = { meow() { console.log('Meow'); } }; cat.meow; → The brackets are missing, so the method is not called. It only refers to the function and nothing runs. Fix: cat.meow();
Q6
const cat = { meow() { console.log('Meow'); } }; meow(); → The method is called like a plain function. It belongs to cat, so it needs the dot: cat.meow();
Section C: Definitions
Q1
greet() { ... } — A method is a function stored as an object property. Write it inside the object, then call it with a dot and brackets: person.greet().
Section D: Fill in the Blanks
Q1
const pet = { speak() { return "Woof"; } }; console.log(pet.speak());
Q2
const pet = { speak() { return "Woof"; } }; console.log(pet.speak());
Q3
const user = { name: "Mia", hello() { return "Hi " + this.name; } };
Q4
const maths = { triple(n) { return n * 3; } };
Q5
const calc = { double(n) { return n * 2; } }; console.log(calc.double(4));
Section E: Spot the Error
Q1
C — A method needs brackets after its name, just like a function. It should be bark() { return "Woof"; }.
Q2
D — Inside a method, properties of the object must be reached with this. Plain word does not exist, so it should be this.word.
Q3
D — Without brackets the method is not called. It just refers to the function itself. It should be car.go().
Q4
B — Members of an object literal are separated by commas, including before a method. It should be name: "Sam",
Q5
A — An object literal with methods is wrapped in curly braces, not square brackets. It should be const pet = {.