prototype
Posté : 18 nov. 2019, 19:58
Bonjour à tous,
Je débute en JS et suis nouveau sur le forum.
Je suis ici car je fais mon premier tp en poo et ça coince.
Ici, j'ai fait une class diaporama avec ses quatre propriétés, show affiche différentes img à chaque qu'on l'appelle et diap appelle show.
C'est ici que j'aurais besoins d'explications car je ne vois pas mon erreur.
Ci dessous le code et merci d'avance pour vos explications:
<body>
<img id="diapo_01" width="250" height="250">
<img id="diapo_02" width="250" height="250">
<img id="diapo_03" width="250" height="250">
<script type="text/javascript">
//constructeur
function Diaporama(cible,tableau,repertoire,duree){
this.Image = document.getElementById("cible");
this.Tableau = tableau;
this.temps = duree;
this.rep_img = repertoire;
this.tabIndex = -1; //compteur
}
// appelle show tous les 3000.
Diaporama.prototype.diap = function(){
setInterval(this.show,this.temps);
};
// boucle show : augmente le compteur, affiche une img et si(...) remet le compteur a -1.
Diaporama.prototype.show = function(){
this.tabIndex ++;
this.Image.src = this.rep_img+this.Tableau[this.tabIndex];
if(this.tabIndex===this.Tableau.length-1){
this.tabIndex = -1;
}
};
// les trois tableau
var tabDiapo_01 = new Array("champ1.jpeg","nivo1.jpg","rom1.jpg","rose1.jpg","rose2.jpg","rose3.jpg");
var tabDiapo_02 = new Array("nivoRom1.jpg","rose3.jpg","rom2.jpg",);
var tabDiapo_03 = new Array("ombre1.jpg","rom2.jpg","rom1.jpg","ombre1.jpg","rose2.jpg");
// instance
var d1 = new Diaporama("diapo_01","tabDiapo_01","foto/","1000");
var d2 = new Diaporama("diapo_02","tabDiapo_02","foto/","2000");
var d3 = new Diaporama("diapo_03","tabDiapo_03","foto/","3000");
d1.prototype = Object.create(Diaporama.prototype);
d2.prototype = Object.create(Diaporama.prototype);
d3.prototype = Object.create(Diaporama.prototype);
d1.prototype.constructor = d1;
d2.prototype.constructor = d2;
d3.prototype.constructor = d3;
d1.diap();
</script>
</body>
Je débute en JS et suis nouveau sur le forum.
Je suis ici car je fais mon premier tp en poo et ça coince.
Ici, j'ai fait une class diaporama avec ses quatre propriétés, show affiche différentes img à chaque qu'on l'appelle et diap appelle show.
C'est ici que j'aurais besoins d'explications car je ne vois pas mon erreur.
Ci dessous le code et merci d'avance pour vos explications:
<body>
<img id="diapo_01" width="250" height="250">
<img id="diapo_02" width="250" height="250">
<img id="diapo_03" width="250" height="250">
<script type="text/javascript">
//constructeur
function Diaporama(cible,tableau,repertoire,duree){
this.Image = document.getElementById("cible");
this.Tableau = tableau;
this.temps = duree;
this.rep_img = repertoire;
this.tabIndex = -1; //compteur
}
// appelle show tous les 3000.
Diaporama.prototype.diap = function(){
setInterval(this.show,this.temps);
};
// boucle show : augmente le compteur, affiche une img et si(...) remet le compteur a -1.
Diaporama.prototype.show = function(){
this.tabIndex ++;
this.Image.src = this.rep_img+this.Tableau[this.tabIndex];
if(this.tabIndex===this.Tableau.length-1){
this.tabIndex = -1;
}
};
// les trois tableau
var tabDiapo_01 = new Array("champ1.jpeg","nivo1.jpg","rom1.jpg","rose1.jpg","rose2.jpg","rose3.jpg");
var tabDiapo_02 = new Array("nivoRom1.jpg","rose3.jpg","rom2.jpg",);
var tabDiapo_03 = new Array("ombre1.jpg","rom2.jpg","rom1.jpg","ombre1.jpg","rose2.jpg");
// instance
var d1 = new Diaporama("diapo_01","tabDiapo_01","foto/","1000");
var d2 = new Diaporama("diapo_02","tabDiapo_02","foto/","2000");
var d3 = new Diaporama("diapo_03","tabDiapo_03","foto/","3000");
d1.prototype = Object.create(Diaporama.prototype);
d2.prototype = Object.create(Diaporama.prototype);
d3.prototype = Object.create(Diaporama.prototype);
d1.prototype.constructor = d1;
d2.prototype.constructor = d2;
d3.prototype.constructor = d3;
d1.diap();
</script>
</body>