Makes prototypical inheritance easy and robust


This is a small script that adds the ability to inherit other functions prototype objects into your own. It makes prototypical inheritance easy and robust.

Due to there not being any documentation yet, you may want to read my JSDoc comments in the source. They will tell you everything you need to know. Here is a quick example to get you going anyway.

// Create the base class. 
function Base() {}
// Add a method. 
Base.prototype.foo = function () {
    return 'Base#foo';
// Create a sub class which inherits from base. 
function Sub() {}
heir.inherit(Sub, Base);
// Mix in some functionality enhancing objects. 
heir.mixin(Sub, events);
heir.mixin(Sub, pooling);
// Change the original method. 
Sub.prototype.foo = function () {
    return [
// Create an instance of Sub and call it's method. 
var s = new Sub();
s.foo(); // Returns "Sub#foo, Base#foo" 

You can load this script into your browser using a normal script tag or AMD. You can also use node.js' require if you are running server side.

The inherit method used to work by cloning and merging multiple prototypes into one. This meant things like instanceof didn't work and you could get into some weird scenarios caused by multiple inheritance.

The new inherit uses the built in prototypical inheritance to provide a much cleaner outcome, as shown in this post about prototypical inheritance. The major change is that you can't inherit from multiple classes any more.

If you still need to have multiple things shared between classes to avoid duplication, you can now use the mixin method to merge objects into your inheritance hierarchies where required.

The concept of _super now works better, but in a completely different way. Thanks to @vejersele in issue #9.

You can obtain a copy by cloning this repository with git, installing through npm or bower. Heir is called heir within both package managers.

If you wish to run the tests you will also need to install Jasmine. You can do this through bower like so.

bower install --dev

Tests are performed using Jasmine in the following browsers.

  • Firefox
  • Chrome
  • Opera
  • Safari
  • IE6+

When testing in the more modern browsers, not Internet Explorer basically, I run it through the very early versions, some midrange versions and the very latest ones too. I don't just do the latest version.

Heir will always be tested and working perfectly in all of them before a release. I will not release anything I think is riddled with bugs. However, if you do spot one, please submit it as an issue and I will get right on it.

This project is given to you under the unlicense, as documented in the UNLICENSE file in this directory. Enjoy.