Prototypes and Inheritance

Rather than classes, JavaScript has prototypes. It uses a system called protypal inheritance. Remember, everything in JavaScript is an object, not a class.

Inheritance is achieved by pointing an object's __proto__ property to another object. The best way to do that is with the Object.create()

// An animal 'class'
function Animal (name) { = name

// Give all future Animals this function
Animal.prototype.greet = function () {
  console.log(`Hello, my name is ${}`)

// A cat 'class'
function Cat (name, breed) {
  // Bind the name to the animal parent object, name)
  this.breed = 'tabby'

// the Object create method allows
// objects to inherit from others
Cat.prototype = Object.create(Animal.prototype)

Cat.prototype.purr = function () {

// Nora has access to all the methods on 
// Cats and on Animals
const nora = new Cat('Nora')
$ node jsbyexample.js
 Hello, my name is Nora
Back to main