Tobias Schneider has another piece of JavaScript Badassery, this time another play at fixing the JavaScript object model. def.js is just over 90 lines long, but it has some cool tricks up it’s sleeve. First, check out this example of it in use:

def ("Person") ({ init: function(name){ this.name = name; }, speak: function(text){ alert(text || "Hi, my name is " + this.name); } }); def ("Ninja") << Person ({ init: function(name){ __super__(name); }, kick: function(){ this.speak("I kick u!"); } }); var ninjy = new Ninja("JDD"); ninjy.speak(); ninjy.kick();

It is all pretty standard stuff until you get to the declaration of the Ninja class, which extends the Person class. Through some trickery using valueOf and bit shifting operations, def.js is able to create a non-standard looking syntax for extending objects that is very readable and intuitive. It also manages to get a pretty simple syntax for running super , through the use of the non-standard but well implemented property of all Function objects, caller . This combination is enough to make an interesting syntax for dealing with classes in JavaScript.

You can check out the code on Github, and hack away on it if you’d like! And as Malte Ubl just said on Twitter:

Now, get to work on your own hacks!