Home

Prototypal Inheritance with Constructors

The "new" keyword and Constructor Functions

new is a reserved keyword in javascript.
The new keyword allows for creating instances of the "thing" that is being made "new".

This new keyword can be leveraged to build constructor functions - functions that can be used to create instances of the new thing.

function Room({w, l, type}){
  this.type = type || 'room';
  this.walls = 4;
  this.doors = 1;
  this.windows = 1;
  this.width = w;
  this.length = l;
}

// add methods to the Room constructor
Room.prototype.getDimensions = function getDimensions(){
  return `This ${this.width}x${this.length} ${this.type} is ${this.width * this.length} sq ft`
}
const kitchen = new Room({ w:22, l:16, type: 'kitchen'})
// Room { type: 'room', walls: 4, doors: 1, windows: 1 }

kitchen.getDimensions()
// 'This 22x16 kitchen is 352 sq ft'

Some details about this:

  • the constructor function above is the Room function
  • the constructor starts with a capital letter (this is by convention)
Tags: