clah

Simple Javascript Inheritance by John Resig

npm install clah
1 downloads in the last day
3 downloads in the last week
40 downloads in the last month

clah

Simple JavaScript Inheritance with bound callbacks

All credit for the inheritance system goes to John Resig. I adapted it to work both with Node.js or in a browser, and added a function to generate bound callbacks.

// define a class
var Person = Class.extend({

  // this is the constructor
  init : function(name) {
    this.name = name;
  },

  // you can use instance properties in your methods
  hello : function() {
    console.log("Hello, I'm " + this.name + "!");
  }
});

// define a subclass
var Pirate = Person.extend({

  // you can override methods
  hello : function() {
    console.log("Ahoy! Me be " + this.name + ".");
  }
});

new Person('Jim').hello();    // #=> "Hello, I'm Jim!"
new Pirate('John').hello();   // #=> "Ahoy! Me be John."

// create a bound callback
var jane = new Person('Jane');
var callback = jane.callback('hello');

// you can use this callback anywhere, it will always be bound to the instance
callback();   // #=> "Hello, I'm Jane!"

Clah is tested with Jasmine and Travis CI.

  • master Build Status
  • develop Build Status

Installation

With NPM:

npm install clah

In a browser:

<script type='text/javascript' src='/path/to/your/assets/clah.min.js'></script>

Download: Production (minified), Development (uncompressed).

Original Blog Post

npm loves you