javascript - Function doesnt see parameter as a array of objects -


i´m having troubles array of objects parameter function.

real codes:

function drawtable(x,y,numero,t,e) {      context.fillstyle = "#ffff66";     context.fillrect(x*146,y*146,55,55);       var color;      if (e[numero].tocado == "rojo") {         color = "#cc3300"     } else if (e[numero].tocado == "azul") {         color = "#0099ff";     } else {         color = "#66ff66";     }      context.fillstyle = color;     if (t==0 && x < 4) {         context.fillrect((x*146)+55,(y*146)+9,91,39);     }     if (t==1){         context.fillrect((x*146)+9,(y*146)+55,39,91);     }      if (x==4) {         if (y<4) {             if (t==1) {                 drawtable(0,y+1,numero+1,0,e);             } else {                 drawtable(0,y,numero,1,e);             }         }     } else {         drawtable(x+1,y,numero+1,t,e);     }     return;      }  socket.on("start", function (data) {     game = data;     drawtable(0,0,0,0,game.elements);    //....  } "data object:" function game() {    this.turno = "";    this.puntuacion_rojo = 0;    this.puntuacion_azul = 0;    this.elements = []; }  "elements built"  this.elements.push({         id:          tipo:          tocado: ,         left:          top:          width:          height:         }); 

the error is:

uncaught typeerror: cannot read property 'tocado' of undefined.

in line

if (e[numero].tocado == "rojo") {  

data isn't empty, tested it, returns server correctly.

so, problem "cosa" "thinks" e isnt array of objects

you're mistaken. cosa isn't ever called. thread stops when uncaught exception thrown. since e undefined, exception on line:

e[0].hello = "hello!"; 

if want access e[0], e should array:

var e = []; 

and if want access e[0].hello, e[0] should object:

var e = []; e[0] = {}; e[0].hello = "hello!": 

or of in 1 line:

var e = [{hello: "hello!"}]; 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -