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.
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).