I don't think there is a "free" way out of the "recursive super" problem you mention.
We can't mess with the this
because doing so would either force us to change prototypes in a nonstandard way, or move us up the proto chain, losing instance variables. Therefore the "current class" and "super class" must be known when we do the super-ing, without passing that responsibility to this
or one of its properties.
There are many some things we could try doing but all I can think have some undesireable consequences:
- Add super info to the functions at creation time, access it using arguments.calee or similar evilness.
Add extra info when calling the super method
$super(CurrentClass).method.call(this, 1,2,3)
This forces us to duplicate the current class name (so we can look up its superclass in some super dictionary) but at least it isn't as bad as having to duplicate the superclass name, (since coupling against the inheritance relationships if worse then the inner coupling with a class' own name)
//Normal Javascript needs the superclass name
SuperClass.prototype.method.call(this, 1,2,3);
While this is far from ideal, there is at least some historical precedent from 2.x Python. (They "fixed" super for 3.0 so it doesn't require arguments anymore, but I am not sure how much magic that involved and how portable it would be to JS)
Edit: Working fiddle
var superPairs = [];
// An association list of baseClass -> parentClass
var injectSuper = function (parent, child) {
superPairs.push({
parent: parent,
child: child
});
};
function $super(baseClass, obj){
for(var i=0; i < superPairs.length; i++){
var p = superPairs[i];
if(p.child === baseClass){
return p.parent;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…