On Oct 12, 10:28 pm, Andy Chu <[email protected]> wrote:
> In Python I have this class hierarchy:
>
> _AbstractSection
> _Section
> _RepeatedSection
> _PredicateSection
>
> What's the best way to do this in JavaScript? The subclasses have to
> call the superclass constructor, which I haven't done before.
That was the reason I did it differently. There really is no class
mechanism in JS itself, but over the years a few ways have been
implemented. If you didn't mind dependencies, then it would be easy.
Though to get that, you'd double the code base. That said, it is not
really all that hard, just that you have to do what other languages do
automatically. This is not working code, but you should get the idea:
var Fruit=function(){};
Fruit.prototype.myMethod=function(){
alert('Fruit is');
};
var Apple=function(){};
// make Apple a class of Fruit
// first save reference to Fruit
Apple.prototype.superclass=Fruit.prototype;
// copy all of Fruit stuff to Apple
for (var name in Fruit.prototype){
Apple.prototype[name]=Fruit.prototype[name];
}
// override parent method
Apple.prototype.myMethod=function(){
// call parent: apply takes scope, then array of args. Could also
use call(this, arg1, arg2,...etc)
this.superclass.myMethod.apply(this,arguments);
alert('Apple');
};
var test= new Apple();
test.myMethod();
For the constructor part, save a named ref call 'constructor' to Fruit
in Apple's prototype. It is something the for loop won't get,
obviously.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JSON
Template" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/json-template?hl=en
-~----------~----~----~----~------~----~------~--~---