A classical JavaScript inheritance helper

Setting up inheritance in JavaScript, you have to do a couple of not-particularly-intuitive steps with the prototype.

I’ma add this guy to my TextExpander snippets:

// Extend the Function object to simplify inheritance
Function.prototype.inheritsFrom = function(parentClass) {
  this.prototype = new parentClass();
  this.prototype.constructor = this;

  // Add a friendly name for the parent class
  this.prototype._super = parentClass.prototype;

  return this;

You have to remember to call this method directly after the constructor, before you do anything else with the extending object’s prototype.