For full details visit my blog post about the standalone library here: http://twologic.com/archives/2006/06/17/javascript-oo-ruby-style/
The following is a code snippet that showcases some of the libraries features. There are more examples over at http://www.twologic.com/repos/inheritance/examples/
Example Usage:
Debug = {
log: function(msg) {
document.writeln(msg);
},
inspect: function() {
var out = '{\r'; // use /r so I can find it over /n
for (var p in joe) {
out += p + ": " + ((joe[p].toString()).replace(/\r/g, '\\r')) + ',\r'
}
out = out.substring(0, out.length - 2);
out += '\r}'
return out;
}
}
Employee = Class.create({
// constructor
initialize: function(name, dept) {
this.name = name;
this.dept = dept;
},
doWork: function() {
return this.name + " is now working...";
}
});
// Manager inherits functionality from Employee
Manager = Class.create(Employee, {
// ruby mixins; mixin all of Debug's methods
include: Debug,
// include: [Debug, Comparable] // would also import Comparable if it was defined
// constructor
initialize: function(name, dept, title) {
this.parent(name, dept); // calls initialize on the parent, Employee
this.title = title;
},
// overrideen method
doWork: function() {
this.log(this.parent() + "at managing the employees.");
}
});
var joe = new Manager("Joe", "Sales", "Sales Manager");
joe.doWork()
Debug.log("Manager = " + joe.inspect());