Extnd
Extend is based off Jon Resig's Simple JavaScript Inhertitance blog post with the added support for multiple inheritance and AMD & CommonJS loaders. It is also lint-free and removes a superfluous check which breaks Closure Compiler. This inheritence system has been tested on large production apps for desktop and mobile.
It is recommended you import extnd.js
using a CommonJS or AMD loader. Otherwise Class
will be added to window
and you could have name collisions.
Usage
For your constructor
method use init
instead.
var Animal = Class;
And to inherit:
var Bird = Animal;
Note in the example above we call the parent method with this._super
. This reference is dynamic in a way that supports multiple inheritance. You can of course call it in any way you would call a normal method:
this;this_super;this_super;
You can string extensions together (multiple inheritance):
var HummingBird = Animal;
It is worth noting that flapsVeryFast
and carefulWithThisObject
variable exists in the class-scope. flapsVeryFast
is a primitive and so it will be copied to all instances. However carefulWithThisObject
is an Object and it will be shared, therefore making a flapCount
incrementor here a bad idea as all instances will increment it. To create variables in instance-scope create them inside a Function e.g. this.carefulWithThisObject = ...
License
Released under the MIT license (see LICENSE for jargon).
Credits
Michal Kot for his multiple inheritance idea