OOP – constructor Method, online
Six activities your students do on their own devices. See who's finished, and their scores.
OOP – constructor Method in one minute
What students need to know
this points to the instance the constructor is building. This is exactly what the constructor method does.
constructor() fires as soon as new ClassName() is called. JavaScript is case-sensitive, so 'constructor' must be lowercase.
Mistakes students make
- 'constructor' must be lowercase - a capital C causes an error.
- Missing 'this.' before name - it should be this.name.
- The block must close with }, not ].
- 'new' must be lowercase - a capital N causes an error.
What students will learn in this module
Every question in Module 48
Quick questions
- Does constructor() run automatically when a new instance is created?AYesBNo
- Does 'this' refer to the instance being created inside constructor()?ANoBYes
- Is 'Constructor' with a capital C valid JavaScript syntax?ANoBYes
- Must 'new' be written in lowercase?AYesBNo
- Can a constructor accept parameters like name or age?ANoBYes
Key terms
Students type the meaning of:
constructor
Match the pairs
constructor(name) { this.name = name; }Match it to what it doesnew Dog("Rex")Match it to what it doesRefers to the new object being created inside a constructorMatch it to what it doesRuns automatically whenever a class is instantiated with newMatch it to what it doesConstructor(name) { this.name = name; }Match it to what it doeslet d = New Dog(5);Match it to what it doesFill in the blanks
___________(name) {Hint: Type the special method that runs when a new instance is created.
this.name = ____;
Hint: Type the parameter name being assigned to this.name.
let myDog = new Dog("___");Hint: Type the string argument passed to the constructor.
___________(color) {Hint: Type the special method that runs when a new instance is created.
this.color = _____;
Hint: Type the parameter name being assigned to this.color.
Spot the error
Question 1
AConstructor(name) {Bthis.name = name;C}
Question 2
Aconstructor(name) {Bname = name;C}
Question 3
Aconstructor(name) {Bthis.name = name;C]
Question 4
AConstructor(age) {Bthis.age = age;C}Dlet d = new Dog(5);
Question 5
Aconstructor(age) {Bthis.age = age;C}Dlet d = New Dog(5);
