Joshua reviewed existing "ugly" prototype approachs such as simple inheritance without super calls, broken attempts at adding super class calling, hard coded super calling, Douglas Crockford's strategy proposed in "Classical Inheritance in JavaScript" (which he found broken and also slow), and finally, a dynamic super calling approach which is fast and works.
This final design leverages a little known feature in JavaScript, although one that is supported by all major browsers. During the execution of any function, you can refer to the arguments that were passed in via the 'arguments' array. That's fairly well known, but the lesser known detail is that the 'arguments' array contains a reference in the property 'callee', which points to the current function that is being executed. This is important, because it's the only way that you can get such a reference since the function reference available via the 'this' object, always refers to the overridden function that is defined in the class at the top of the hierarchy.Earlier this year, Dean Edwards published specialized base class for doing OO in Javascript that also addresses these problems and is being considered for inclusion in 2.x versions of the popular Prototype Javascript framework.
Community comments
"Lazy" JavaScript inheritance
by Andrew Sazonov,
JavaScript Inheritance
by Michael Heliso,
"Lazy" JavaScript inheritance
by Andrew Sazonov,
Your message is awaiting moderation. Thank you for participating in the discussion.
There is another approach to implement JavaScript inheritance - a “lazy” inheritance which has all benefits of “prototype” based approach like typed classes, but also eliminates necessity to declare external scripts in proper order and automatically resolves and loads (if necessary) dependencies to external scripts that contains related classes.
This approach is supported by JSINER library - you can find more about it on www.soft-amis.com/jsiner/inheritance.html
JavaScript Inheritance
by Michael Heliso,
Your message is awaiting moderation. Thank you for participating in the discussion.
Hi,
I have tried an different techniques related to JavaScript inheritance subject.
The code and the explanation are to long to post them here so if anyone is interested to take a look over it you can find it at www.dotnetcaffe.net under JavaScript category. Fell free to criticize the code in any way you want...just don't flame :).