Skip to content

Instantly share code, notes, and snippets.

View kimhogeling's full-sized avatar

Kim Hogeling kimhogeling

  • interpersonal GmbH
  • Hamburg
View GitHub Profile
@kimhogeling
kimhogeling / javascript_inheritance.md
Last active August 29, 2015 14:20
JavaScript Inheritance

TL;DR. Take me to the example!

Prototype chaining

JavaScript inheritance is all about prototype chaining. To create one, simply set the prototype of an instance of a subtype equal to the one of an instance of the supertype. I use Object.create() for that. There is one problem though: how to inherite the supertype's properties?. This is where constructor stealing comes in.

Constructor stealing

The subtype can inherit supertypes properties, by calling the constructor of the supertype. I use call() for that. The combination of prototype chaining and constructor stealing is called pseudo classical inheritance, because it is not exactly like in class based languages, but does behave the same.