Constructor Functions

Page section: posts

Constructor functions start @ 59mins on vid 1.

Constructor functions are functions used to create objects in a less verbose way than simply writing out the object. They’re useful when you have need to create multiple objects with the same form, for instance firstName, lastName, dateOfBirth.

  1. Contstructor functions should be written in Pascal notation rather than Camel. These are like camelCase but start with a capital letter, eg. PersonOne, MyVar.
  2. The this keyword references the object that is executing the current function. The value of this is evaluated during the run-time, depending on the context. 1

The job of constructor functions is to create an object.

// Constructor function

function Person(firstName, lastName, dob) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.dob = new Date(dob);
    this.getBirthYear = function() {
        return this.getFullYear();  // getFullYear is a method of the date object
    }
    this.getFullName = function () {
        return `${this.firstName} ${this.lastName}`;
    }
}

// Instantiate object

const person1 = new Person('Fred', 'Bloggs', '12-10-1977');
const person2 = new Person('Richard', 'Harris', '27-4-1982');

// This creates a person object. Check in the console

console.log(person1);
console.log(person2.firstName);
console.log(person2.dob.getFullYear());

// Looks like an object literal but is prefixed with the name Person

// Using the function created getBirthYear

console.log(person1.getBirthYear());

this

By default the this keyword references the global object (window). However when used in an object, as a method of that object, it refers to that object.

const video {
    title: 'Yard Dash',
    play() {
        console.log(this);
    }
};

video.play();

However this does not work inside arrow functions. Instead it looks for the next object outside which maybe the window object.

If you’re unsure you can always try using console.log(this) to find out which object this is referencing.

Here’s good explantion…

View contents of: /static/js/pure-data.js
// This file contains no programming just objects, variables and arrays

const primates = [
  "mangabey",
  "drill",
  "colobus",
  "gelada",
  "linsang",
  "lutung",
  "talapoin",
  "tamarin",
  "titi",
  "guenon",
  "guereza",
  "kipunji",
  "grivet",
];

const viverids = ["binturong", "civet", "falanouc", "genet"];

const dinosaurs = [
  {
    name: "Baryonyx",
    meaning: "Heavy claw",
    family: "Spinosaur",
    period: "Early cretaceous",
    country: "UK",
    year: 1983,
  },
  {
    name: "Bolong",
    meaning: "long dragon",
    family: "Hadrosaur",
    period: "Early cretaceous",
    country: "China",
    year: 2010,
  },
  {
    name: "Opisthocoelicaudia",
    meaning: "posterior cavity tail",
    family: "Titansaur",
    period: "Late cretaceous",
    country: "Mongolia",
    year: 1965,
  },
  {
    name: "Coelophysis",
    meaning: "Hollow form",
    family: "theropod",
    period: "Late triassic",
    country: "USA",
    year: 1881,
  },
  {
    name: "Epidexipteryx",
    meaning: "Hu Yaoming's dragon",
    family: "theropod",
    period: "Jurassic",
    country: "Inner Mongolia",
  },
];

// Bond films using object constructor

function BondFilm(title, year, actor, num, bondGirl, imdb) {
  (this.title = title),
    (this.year = year),
    (this.actor = actor),
    (this.num = num),
    (this.bondGirl = bondGirl),
    (this.imdb = imdb);
}

const drNo = new BondFilm(
  "Doctor No",
  1962,
  "Sean Connery",
  1,
  "Ursula Andress",
  7.2
);

const fromRussiaWithLove = new BondFilm(
  "From Russia with Love",
  1963,
  "Sean Connery",
  2,
  "Daniela Bianchi",
  7.4
);

const goldfinger = new BondFilm(
  "Goldfinger",
  1964,
  "Sean Connery",
  3,
  "Honor Blackman",
  7.7
);

const thunderball = new BondFilm(
  "Thunderball",
  1965,
  "Sean Connery",
  4,
  "Claudine Auger",
  7
);

const youOnlyLiveTwice = new BondFilm(
  "You Only Live Twice",
  1967,
  "Sean Connery",
  5,
  "Akiko Wakabayashi",
  6.8
);

const onHerMajestysSecretService = new BondFilm(
  "On Her Majestys Secret Service",
  1969,
  "George Lazenby",
  6,
  "Diana Rigg",
  6.7
);

const diamondsAreForever = new BondFilm(
  "Diamonds are Forever",
  1971,
  "Sean Connery",
  7,
  "Jill St John",
  6.6
);

const liveAndLetDie = new BondFilm(
  "Live and Let Die",
  1973,
  "Roger Moore",
  8,
  "Jane Seymour",
  6.8
);

const theManWithTheGoldenGun = new BondFilm(
  "The Man with the Golden Gun",
  1974,
  "Roger Moore",
  9,
  "Britt Eckland",
  6.7
);

const theSpyWhoLovedMe = new BondFilm(
  "The Spy Who Loved Me",
  1977,
  "Roger Moore",
  10,
  "Barbara Bach",
  7.1
);

const moonraker = new BondFilm(
  "Moonraker",
  1979,
  "Roger Moore",
  11,
  "Lois Chiles",
  6.3
);

const forYourEyesOnly = new BondFilm(
  "For Your Eyes Only",
  1979,
  "Roger Moore",
  12,
  "Carole Bouquet",
  6.7
);

const octopussy = new BondFilm(
  "Octopussy",
  1983,
  "Roger Moore",
  13,
  "Maud Adams",
  6.5
);

const aViewToAKill = new BondFilm(
  "A View to a Kill",
  1985,
  "Roger Moore",
  14,
  "Tanya Roberts",
  6.3
);

const theLivingDaylights = new BondFilm(
  "The Living Daylights",
  1987,
  "Timothy Dalton",
  15,
  "Maryam d'Abo",
  6.7
);

const licenceToKill = new BondFilm(
  "Licence To Kill",
  1989,
  "Timothy Dalton",
  16,
  "Carey Lowell",
  6.6
);

const goldenEye = new BondFilm(
  "Golden Eye",
  1995,
  "Pierce Brosnan",
  17,
  "Izabella Scorupco",
  7.2
);

const tomorrowNeverDies = new BondFilm(
  "Tomorrow Never Dies",
  1997,
  "Pierce Brosnan",
  18,
  "Michelle Yeoh",
  6.5
);

const theWorldIsNotEnough = new BondFilm(
  "The World is Not Enough",
  1999,
  "Pierce Brosnan",
  19,
  "Sophie Marceau",
  6.4
);

const dieAnotherDay = new BondFilm(
  "Die Another Day",
  2002,
  "Pierce Brosnan",
  20,
  "Halle Berry",
  6.1
);

const casinoRoyale = new BondFilm(
  "Casino Royale",
  2006,
  "Daniel Craig",
  21,
  "Eva Green",
  8
);

const quantumOfSolace = new BondFilm(
  "Quantum of Solace",
  2008,
  "Daniel Craig",
  22,
  "Olga Kurylenko",
  6.6
);

const skyfall = new BondFilm(
  "Skyfall",
  2012,
  "Daniel Craig",
  23,
  "Naomie Harris",
  7.8
);

const spectre = new BondFilm(
  "Spectre",
  2015,
  "Daniel Craig",
  24,
  "Léa Seydoux",
  6.8
);

const noTimeToDie = new BondFilm(
  "No Time to Die",
  2021,
  "Daniel Craig",
  25,
  "Ana de Armas",
  7.4
);

const jamesBondFilms = [
  drNo,
  fromRussiaWithLove,
  goldfinger,
  thunderball,
  youOnlyLiveTwice,
  onHerMajestysSecretService,
  diamondsAreForever,
  liveAndLetDie,
  theManWithTheGoldenGun,
  theSpyWhoLovedMe,
  moonraker,
  forYourEyesOnly,
  octopussy,
  aViewToAKill,
  theLivingDaylights,
  licenceToKill,
  goldenEye,
  tomorrowNeverDies,
  theWorldIsNotEnough,
  dieAnotherDay,
  casinoRoyale,
  quantumOfSolace,
  skyfall,
  spectre,
  noTimeToDie,
];