1 pièce(s) jointe(s)
[SRC] héritage javascript, et surdéfinition des méthodes
définissé d'abord vos classe , avec la méthode $class(prototype, classAhériter), ou class abstraite avec la méthode $abstract(prototype, classAhériter)
puis, dans vos fonction de prototype, si vous voulais avoir accès a la méthode parent du même nom, (surdéfinit) faite this.$super([arguments]) , ou d'un autre nom this.$super('autrefunc',[arguments]])
voiçi un exemple d'utilisation
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
/*definition des classes abstraite servant de base pour l'héritage*/
var vehicule =$abstract({
constructor : function(){//se nomme ainsi pour pouvoir etre apellé par les super qui dériveront
this.stringAvance='';//ini la varaible string
},
toStringAvance: function(){
this.stringAvance+=this.stringType+' avance ';
}
});
var vehiculeAMoteur = $abstract({
constructor : function(){
this.$super();//appelle la méthode surdéfinit
},
toStringAvance: function(chevaux){
this.$super();//appelle la méthode surdéfinit
this.stringAvance+='avec un moteur de '+chevaux+' chevaux';
}
},vehicule);
var voiture = $class({
constructor : function(chevaux){
this.$super();//appelle la méthode surdéfinit
this.chevaux=chevaux;
this.toStringAvance();
},
toStringAvance: function(){
this.$super([this.chevaux]);//appelle la méthode surdéfinit avec un arguments
alert(this.stringAvance)
},
stringType : 'La voiture'
},vehiculeAMoteur);
var camion = $class({
constructor : function(chevaux){
this.$super();//appelle la méthode surdéfinit
this.chevaux=chevaux;
this.toStringAvance();
},
toStringAvance: function(){
this.$super([this.chevaux]);//appelle la méthode surdéfinit avec un arguments
alert([this.stringAvance])
},
stringType : 'Le camion'
},vehiculeAMoteur);
new voiture(120);
new camion(1000); |
si vous avez des questions, hésitez po! a++