OOP – Methods, online

Six activities your students do on their own devices. See who's finished, and their scores.

OOP – Methods in one minute

What students need to know

This is exactly how a method is written inside a class body. this lets a method access that instance's own properties.

Methods define behavior that instances of the class can use. Methods are written as name() { ... }, without 'function'.

Mistakes students make

  • Method names are case-sensitive - it should be bark(), not Bark().
  • Calling a method needs parentheses: myCat.meow().
  • Class methods skip the 'function' keyword - just write sing() {.
  • Class methods skip the 'function' keyword - just write quack() {.

Discover other journeys with this module

What students will learn in this module

Every question in Module 49

Quick questions

  1. Is a method a function declared inside a class body?ANoBYes
  2. Do class methods use the 'function' keyword?AYesBNo
  3. Is a method called on an instance using dot notation?AYesBNo
  4. Does calling a method require parentheses?AYesBNo
  5. Can 'this' be used inside a method to access the instance's properties?AYesBNo

Key terms

Students type the meaning of:

  • method

Match the pairs

bark() { console.log(this.name + " says woof"); }Match it to what it does
myDog.bark();Match it to what it does
A function declared inside a class, without 'function'Match it to what it does
Refers to the instance a method is called onMatch it to what it does
function bark() { console.log("woof"); }Match it to what it does
myDog.bark;Match it to what it does

Fill in the blanks

  1. ____() {
    console.log(this.name + " says woof");
    }

    Hint: Type the method name declared inside the class.

  2. myDog.____();

    Hint: Type the method name being called on myDog.

  3. ____() {
    console.log(this.color);
    }

    Hint: Type the method name declared inside the class.

  4. myCar.____();

    Hint: Type the method name being called on myCar.

  5. console.log(____.name + " says woof");

    Hint: Type the keyword that refers to the current instance.

Spot the error

Question 1

AmyDog.Bark();Blet myDog = new Dog();Cclass Dog {Dbark() { console.log(1); }E}

Question 2

AmyCat.meow;Blet myCat = new Cat();Cclass Cat {Dmeow() { console.log(2); }E}

Question 3

Aclass Bird {Bfunction sing() {Cconsole.log("tweet");D}E}

Question 4

Aclass Duck {Bfunction quack() {Cconsole.log("quack");D}E}

Question 5

Aclass Fish {Bswim() {Cconsole.log("splash")D}E}