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.
PREVIOUS · MODULE 47OOP – Classes & Objects
YOU ARE HERE · MODULE 48OOP – constructor Method
NEXT · MODULE 49OOP – Methods

Discover other journeys with this module

What students will learn in this module

Every question in Module 48

Quick questions

  1. Does constructor() run automatically when a new instance is created?AYesBNo
  2. Does 'this' refer to the instance being created inside constructor()?ANoBYes
  3. Is 'Constructor' with a capital C valid JavaScript syntax?ANoBYes
  4. Must 'new' be written in lowercase?AYesBNo
  5. 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 does
new Dog("Rex")Match it to what it does
Refers to the new object being created inside a constructorMatch it to what it does
Runs automatically whenever a class is instantiated with newMatch it to what it does
Constructor(name) { this.name = name; }Match it to what it does
let d = New Dog(5);Match it to what it does

Fill in the blanks

  1. ___________(name) {

    Hint: Type the special method that runs when a new instance is created.

  2. this.name = ____;

    Hint: Type the parameter name being assigned to this.name.

  3. let myDog = new Dog("___");

    Hint: Type the string argument passed to the constructor.

  4. ___________(color) {

    Hint: Type the special method that runs when a new instance is created.

  5. 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);