Ver Mensaje Individual
  #1 (permalink)  
Antiguo 11/06/2014, 04:12
Pantaláimon
 
Fecha de Ingreso: julio-2006
Ubicación: Barcelona
Mensajes: 244
Antigüedad: 17 años, 9 meses
Puntos: 32
Comportamiento extraño: Require.js y herencia javascript

Buenas.

Editado: el título habla de Require.js pero lo he obviado y he simplificado el código, pues el problema es inherente al uso de Require.js

El problema está con dos clases TableX y OutputX que heredan de NodeX. Este es el código:
Código Javascript:
Ver original
  1. function NodeX(){};
  2.  
  3.     function OutputX(){}
  4.  
  5.     OutputX.prototype = NodeX.prototype;
  6.     OutputX.prototype.foo = function(){
  7.         console.log("OutputX.prototype.foo");
  8.     }
  9.  
  10.     function TableX(){};
  11.  
  12.     TableX.prototype = NodeX.prototype;
  13.  
  14.     TableX.prototype.foo = function(){
  15.         console.log("TableX.prototype.foo");
  16.     }
  17.     TableX.prototype.bar = function(){
  18.         console.log("TableX.prototype.bar");
  19.     }
  20.  
  21.     var o = new OutputX;
  22.     o.foo();
  23.     o.bar();

En principio como OutputX no extiende a la clase TableX, o.foo() no debería devolver "TableX.prototype.foo" por consola y o.bar() debería dar un error. Sin embargo no es este el caso.

¿Que se me está escapando para no entender este comportamiento, a mi ver, tan extraño?

Un saludo y gracias!
__________________
github.com/xgbuils | npm/xgbuils

Última edición por Pantaláimon; 11/06/2014 a las 04:56