Simple JavaScript Inheritance by John Resig with our improves
JavaScript HTML
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Simple JavaScript Inheritance by John Resig with our improvements.

Classes can be created by calling Class.extend(name,{ ... }). The main things js-inherit get you are easy inheritance, a constructor method called init(), dynamic addition of a this._super method when a method is overloaded (be careful with this as it adds some overhead to method calls.) Calls to instanceof also all work as you'd hope.

By convention, classes should be capitalized, so if you wanted to create a new class, you'd write:

Class.extend("MyClass",{ ... });

Using js-inherit

Basic usage:

Class.extend("Animal", {
    init: function (name) { = name;
    speak: function () {
    fly: function () {
        console.log("Can't fly");

Animal.extend("Butterfly", {
    speak: function () {
        console.log( + " the Butterfly");
    fly: function () {

var Noname = Animal.extend({
    speak: function () {
        console.log( + " the Noname");
    fly: function () {
        console.log("Not sure...");

var randomAnimal = new Animal("John");
var io = new Butterfly("Io");
var noname = new Noname("Creature");; // Logs "Can't fly";           // Logs "Flying";       // Logs "Not sure..."

randomAnimal.speak(); // Logs "John"
io.speak();           // Logs "Io the Butterfly"
noname.speak();       // Logs "Creature the Noname"

console.log(randomAnimal instanceof Animal);    // true
console.log(randomAnimal instanceof Butterfly); // false
console.log(io instanceof Animal);              // true
console.log(io instanceof Butterfly);           // true
console.log(noname instanceof Animal);          // true

console.log(randomAnimal.isA("Animal"));        // true
console.log(io.isA("Butterfly"));               // true
console.log(io.isA("Animal"));                  // false
console.log(noname.isA("Noname"));              // false
console.log(noname.isA("Animal"));              // true

If you wanted to call a super-classes’ constructor you could do that with this technique:

Class.extend("Person", {
    init: function(isDancing){
        this.dancing = isDancing;

Person.extend("Ninja", {
    init: function(){

var p = new Person(true);
console.log(p.dancing); // true

var n = new Ninja();
console.log(n.dancing); // false



extend (className, properties, [classMethods])

Create a new Class that inherits from this class

  • className String
  • properties Object - hash of properties (init() will be the constructor)
  • [classMethods] Object, optional - optional class methods to add to the class


isA (className)

See if a object is a specific class

  • className String - class to check against




  • Initial Release