Skip to content

Instantly share code, notes, and snippets.

@alan-ps
Created July 18, 2019 21:21
Show Gist options
  • Save alan-ps/b7cbb0a444d9c539e26c1f57c3f82d3c to your computer and use it in GitHub Desktop.
Save alan-ps/b7cbb0a444d9c539e26c1f57c3f82d3c to your computer and use it in GitHub Desktop.

Functional inheritance

function Animal() {
  var that = {};
  that.eats = true;
  return that;
}

function Dog() {
  var that = Animal();
  that.runs = true;
  return that;
}

var dog = Dog();
console.log(dog.eats); // true

Functional inheritance by a constructor

function Animal() {
  this.eats = true;
}

function Dog() {
  Animal.call(this);

  this.runs = true;
}

var dog = new Dog();
console.log(dog.eats); // true

Prototype inheritance

function Animal() {
  this.eats = true;
}

function Dog() {
  this.runs = true;
}

Dog.prototype = new Animal();

var dog = new Dog();
console.log(dog.eats); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment