Bonjours à tous,
J'essaie de stocker différents objets de même type (classe) dans un tableau.
Lorsque j'essaie d'appeler une méthode de chacun des objets, en les accédant via le tableau qui les stocke (lui-même attribut d'un objet), j'obtiens une erreur javascript ("this.totos[i].describe is not a function") . Voici le code en question :
Si quelqu'un a une idée...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 function toto(a, b) { this.a = a; this.b = b; if( typeof toto.initialized == "undefined" ) { toto.prototype.describe = function() { console.log(this.a + ' / ' + this.b); } toto.initialized = true; } } function totoManager() { this.totos = new Array(); if( typeof totoManager.initialized == "undefined" ) { totoManager.prototype.fullInfo = function() { for (var i = 0; i < this.totos.length; i++) { this.totos[i].describe(); } } toto.initialized = true; } } var tab = new totoManager(); tab.totos.push(new toto(1,2)); tab.totos.push(new toto(3,4)); tab.totos.push(new toto(5,6)); tab.fullInfo();![]()
Partager