Skip to content
Shubham JangleResources
JavaScript

Explore topics

WTF is __proto__? (constructor functions & ES6 Classes)

Watch the walkthrough
Open on Instagram

Revision clarifications

A constructor's prototype property becomes the instance's prototype when called with new. In a derived class, call super before using this. Vehicle.call(this) initializes fields; setting Car.prototype's prototype supplies inherited methods.

The source notes below are copied unchanged apart from line endings. Examples are independent; some deliberately throw or depend on browser markup.

Original HackTheCode notes

Read the original Markdown

/\*_ 1. Constructors Functions _/;
// Vehicle constructor function
function Vehicle() {
  this.wheels = 2;
}

// Add the ready method to Vehicle's prototype
Vehicle.prototype.ready = function () {
  return "Ready to go!";
};

// Car constructor function
function Car() {
  Vehicle.call(this); // Call the Vehicle constructor so Vehicle's varibles are initialized, similar to super(this);
  this.wheels = 4;
}

// Add the boost method to Car's prototype
Car.prototype.boost = function () {
  return "boosted!";
};

Object.setPrototypeOf(Car.prototype, Vehicle.prototype); // Set up inheritance - Similar to extends keyword in other languages

// Create an instance of Car
const porche = new Car();

console.log(porche); // { wheels: 4 }
console.log(porche.ready()); // "Ready to go!"
console.log(porche.boost()); // "boosted!"
/\*_ 2. ES6 Classes - inheritance _/;
class Vehicle {
  constructor() {
    this.wheels = 2;
  }

  ready() {
    return "Ready to go!";
  }
}

class Car extends Vehicle {
  constructor() {
    super(); // as we are using this.wheels we need to have super() first so this.wheels is correctly initialized in Vehicle.
    this.wheels = 4;
  }

  boost() {
    return "boosted!";
  }
}

const tesla = new Car();
console.log(tesla); // { wheels: 4 }
console.log(tesla.ready());
console.log(tesla.boost());