Ver Mensaje Individual
  #2 (permalink)  
Antiguo 22/12/2013, 16:03
DoHITB
 
Fecha de Ingreso: abril-2012
Ubicación: 41°37′00″N, 00°37′00″E
Mensajes: 462
Antigüedad: 12 años, 1 mes
Puntos: 33
Respuesta: Valores "apilados" me dan error

Cuando he publicado el topic no estaba muy inspirado...

Después de un rato en plena inspiración he encontrado un modo bastante decente de llevar a cabo mi función:

Para ello, primero he cambiado las variables del objeto:

Código Javascript:
Ver original
  1. var miObjeto = function(){
  2.         /*aquí variables que ya habían*/
  3.         this.stackI5 = new Array();
  4.         this.stackJ = new Array();
  5.         this.stackI5I = 0;
  6.         this.stackJI = 0;
  7. }

Fialmente, emulamos un "stack", tal que:

Código Javascript:
Ver original
  1. this.add=function(fields,e,space,deep){
  2.         this.stackI5I++;
  3.         this.stackJI++;
  4.         this.stackI5[this.stackI5I] = 0;
  5.         this.stackJ5[this.stackJI] = 0;
  6.  
  7.         for(this.stackI5[this.stackI5I]=0;this.stackI5[this.stackI5I]<fields.length;this.stackI5[this.stackI5I]++){
  8.                 for(this.stackJ5[this.stackJI]=0;this.stackJ5[this.stackJI]<e.length;this.stackJ5[this.stackJI]++){
  9.                         if(e.item(this.stackJ5[this.stackJI]).nodeName==fields[this.stackI5[this.stackI5I]]){
  10.                                 if(deep){
  11.                                         this._add(e.item(this.stackJ5[this.stackJI]).firstChild, space+":"+fields[this.stackI5[this.stackI5I]]);
  12.                                 }else{
  13.                                         this._add(e.item(this.stackJ5[this.stackJI]), space+":"+fields[this.stackI5[this.stackI5I]]);
  14.                                 }
  15.  
  16.                                 break;
  17.                         }
  18.                 }
  19.         }
  20.  
  21.         this.stackI5[this.stackI5I] = 0;
  22.         this.stackJ5[this.stackJI] = 0;
  23.         this.stackI5I--;
  24.         this.stackJI--;
  25. };

De aquí a destacar:

* El código antiguo simplemente sustituye "i5" por "this.stackI5[this.stackI5I", y "j" por "this.stackJ[this.stackJI]".
* El separar el incremento y decremento de los índices en separado es para evitar warnings en el JS-Lint
* El devolver los contadores a 0 antes de decrementar es para evitar que se acumule "basura" (para que no guarde valores distintos a 0 cuando acabe el bucle).

Dejo esto aquí como referencia a quién le pudiese servir.

Un saludo.