OOP – Methods, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
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() {.
PREVIOUS · MODULE 48OOP – constructor Method
YOU ARE HERE · MODULE 49OOP – Methods
NEXT · MODULE 50OOP – Inheritance (extends)
What students will learn in this module
Every question in Module 49
Quick questions
- Is a method a function declared inside a class body?ANoBYes
- Do class methods use the 'function' keyword?AYesBNo
- Is a method called on an instance using dot notation?AYesBNo
- Does calling a method require parentheses?AYesBNo
- 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 doesmyDog.bark();Match it to what it doesA function declared inside a class, without 'function'Match it to what it doesRefers to the instance a method is called onMatch it to what it doesfunction bark() { console.log("woof"); }Match it to what it doesmyDog.bark;Match it to what it doesFill in the blanks
____() { console.log(this.name + " says woof"); }Hint: Type the method name declared inside the class.
myDog.____();
Hint: Type the method name being called on myDog.
____() { console.log(this.color); }Hint: Type the method name declared inside the class.
myCar.____();
Hint: Type the method name being called on myCar.
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}
