During the last weeks, I’ve been trying to figure out how I could better write melonJS in order to be more OO oriented (at least in a cleaner way) and to benefit from a simple multiple inheritance mechanism.
I won’t go through all of them in details (there is enough posts on the web about them), but basically I came out with the following possible (and popular) solutions :
- Prototypal based inheritance (through some helper functions)
- Closure Based inheritance (trough some helper functions)
- Using Prototype.js library
- Using Base2 library
- Using Motools library
- Using John Resig Technique (based on Prototype and Base2)
Initially the fight was for me ”Propotypal inheritance versus the world”, as it’s lighter, uses less memory and most of all comes in natural javascript flavor. And with this solution being the most “native” one, it’s also easier to integrate with other libraries.
However, when using inheritance and multiple inheritance a lot (like I’m trying to do), source code can really become a mess, as this solution is also the most verbose one, and some helpers are definitely needed to avoid some annoying issues (like constructors being called with no parameters when extending an Object).
So I’ve been looking around, seeing what was going on, trying to figure out which solution was the most reliable, but also the most popular, made some tests, and finally found this page (among others).
While it was clear for me that using library based inheritance would bring the performance down, I was surprised to see the difference of performances when accessing method ! Of course the test seems a bit old, but seeing the technical explanation behind this result, there is no reason for me it would be different today.
So now I have library based solution that should ease my life with code writing (more OO oriented, and much more clean code) but also giving faster method access. Of course memory footprint is higher, and object creation is slower, but actually who cares (when dealing with games, you just basically create your objects when loading your levels).
At the end the question was : which solution to choose ?
And I finally choosed John Resig one, why ?
- It’s anyway based on both prototype.js and base2 (so I can assume it brings the best of both)
- it’s the most compact solution,with only 25 lines of code
- Very good general feedback, and seems to be working pretty well
- Seems to give the best compromise in terms of size and perfomances
That’s all guys, however I would be very interested to get your feedback about this, this post is the conclusion of my own analysis on the subject, but if you have any other arguments (or better solutions), I would be very glad to read them as well.
See you soon, in the mean time, I got some work to do


