Comunidad de diseño web y desarrollo en internet online

Problema Herencia

Citar            
MensajeEscrito el 24 Ene 2012 03:11 am
Hola!! Tengo el siguiente problema.

Tengo una ClaseA y una ClaseB, ClaseB hereda de ClaseA, pero por alguna razon el Array this.aa es el mismo para ámbos, ya que en el test se van acumulando las letras "E" en el array.
Alguien sabe como solucionarlo?


function ClaseA()
{
this.aa = [];

this.init = function()
{
}

this.addChild = function()
{
this.aa.push("E");
}

this.init();
}

ClaseB.prototype = new ClaseA();
function ClaseB()
{
this.init = function()
{
this.addChild();
alert(this.aa);
}
this.init();
}

var a = new ClaseB();
var b = new ClaseB();

Por lucasmoyano

Claber

1960 de clabLevel

22 tutoriales

Genero:Masculino  

Developer

chrome
Citar            
MensajeEscrito el 24 Ene 2012 03:48 am
jaja ya lo solucione :D

Eh aqui la respuesta:

function ClaseA()
{
this.aa = [];

this.init = function()
{
alert("Created");
}

this.addChild = function()
{
this.aa.push("E");
}

this.init();
}

ClaseB.prototype = ClaseA;
function ClaseB()
{
this.init = function()
{
ClaseA.call(this);
this.addChild();
alert(this.aa);
}
this.init();
}

var a = new ClaseB();
var b = new ClaseB();

Por lucasmoyano

Claber

1960 de clabLevel

22 tutoriales

Genero:Masculino  

Developer

chrome

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.