Domanda

sto facendo OO javascript per la prima volta. Ho letto di eredità e prototipo e ho pensato che avevo incrinato. Fino a quando ho scoperto questo piccolo esempio.

function TestObject(data)
{
    this.test_array = [];
    this.clone_array = [];

    this.dosomestuff = function()
    {
        for(var i=0; i<this.test_array.length; i++)
        {
            this.clone_array[i]=this.test_array[i];
        }
    }   

    this.__construct = function(data)
    {
        console.log("Testing Object Values" ,this.clone_array);
        this.test_array = data;
    };
}

TestObject2.prototype = new TestObject();

function TestObject2(data)
{
    this.__construct(data);
    this.dothings = function()
    {
        this.dosomestuff();
    }
}

Se faccio la seguente:

var foo = new TestObject2([1,2,3,4]);
foo.dothings();
var bar = new TestObject2([4,5,6]);
bar.dothings();

mi aspetto la console per mostrare:

Testing Object Values, []
Testing Object Values, []

Tuttavia si vede:

Testing Object Values, []
Testing Object Values, [1,2,3,4]

Il problema è, naturalmente, questa chiamata:

TestObject2.prototype = new TestObject();

Come faccio ad avere le variabili madri in TestObject di "reset" diverso da loro reimpostare manualmente nel metodo __construct?

C'è un altro modo di TestObject2 di ereditare tutti i valori / metodi da TestObject e per "nuova" di comportarsi come ci si aspetta in un modo PHP OO? (Io sono sicuro che il modo in cui JS sta facendo questo è davvero strano, come se il mio cervello mi inganna presso l'Università Java funziona come PHP a questo proposito)

È stato utile?

Soluzione

In sostanza

TestObject2.prototype = new TestObject(); 

pone una singola istanza del TestObject nella catena di prototipi di TestObject2. Così, tutte le istanze di TestObject2 modificheranno la stessa proprietà singola istanza prole in TestObject. Se si inserisce un'altra console.log nel construtor di TestObject si noterà solo sempre chiamato una volta!

Maggiori dettagli sul tuo problema esatto può essere trovato qui .

È necessario chiamare il costruttore di TextObject dall'interno del costruttore TextObject2 in questo modo:

function TestObject2(data)
{
    TestObject.call( this, data);
    this.__construct(data);
    this.dothings = function()
    {
        this.dosomestuff();
    }
}

Con il richiamo della funzione di costruzione TestObject e l'esecuzione nel campo di applicazione del (questo è) il nuovo oggetto TestObject2, crea tutti gli elementi di TestObject in oggetto TestObject2.

Altri suggerimenti

hai già cercato di definire this.clone_array = []; in TestObject2 invece?

function TestObject2(data)
{
    this.clone_array = [];
    this.__construct(data);
    this.dothings = function()
    {
        this.dosomestuff();
    }
}

Credo che

TestObject2.prototype = new TestObject();

è prevalente il costruttore di TestObject2.

Prova

function TestObject2(data)
{
    TestObject.call( this, data);
    this.__construct(data);
    this.dothings = function()
    {
        this.dosomestuff();
    }
}


TestObject2.prototype = new TestObject();
TestObject2.prototype.constructor = TestObject2;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top