[ vujkev @ 11.08.2012. 23:44 ] @
Hteo bih da nasledim neki generički objekat, ali da svaki child objekat ima svoj niz definisan u parent-u. U primeru ispod kad kreiram Child "x2", on automatski ima sve vrednosti u nizu "collection"

Code (javascript):

function Parent() {
    var collection = [];
    this.add = function (i) {
        collection.push(i);
    }

    this.getCount = function () { return collection.length }
}

function Child() {
    Parent(this);
}
 
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var x1 = new Child();
x1.add(1);
var x1Count = x1.getCount();

var x2 = new Child();
x2.add(1);
var x2Count = x1.getCount();
var x2Count = x1.getCount();
 


Dakle kako napraviti child ili parent tako da kad kažem "new Child()" taj objekat ima svoj (prazan) "collection" niz

[ 574nk3 @ 12.08.2012. 08:29 ] @
Cao,

U funkciji Child je potrebno da pozoves funkciju Parent sto vec i radis. Medjutim moras da prosledis drugi kontekst toj funkciji. Kontekst objekta Child. U parent funkciji niz definisi sa this.collection umesto var collection.

Evo ispravljenog koda.

Code:
function Parent() {
    this.collection = [];
    this.add = function (i) {
        this.collection.push(i);
    }

    this.getCount = function () { return this.collection.length }
}

function Child() {
    Parent.call(this);
}
 
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var x1 = new Child();
x1.add(1);
var x1Count = x1.getCount();

var x2 = new Child();
x2.add(2);
x2.add(3);

var x1Count = x1.getCount();
var x2Count = x2.getCount();

console.log(x1Count);
console.log(x2Count);

console.log(x1["collection"]);
console.log(x2["collection"]);


Jos jedna bitna stvar. Sve funkcije Parent objekta stavi na prototip kako ne bi imao nove instance funkcija za svaki novokreirani Child objekat. Ovako:

Code:

function Parent() {
    this.collection = [];
}

Parent.prototype.add = function (i) {
    this.collection.push(i);
}

Parent.prototype.getCount = function () { 
    return this.collection.length;
}

function Child() {
    Parent.call(this);
}
 
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var x1 = new Child();
x1.add(1);
var x1Count = x1.getCount();

var x2 = new Child();
x2.add(2);
x2.add(3);

var x1Count = x1.getCount();
var x2Count = x2.getCount();

console.log(x1Count);
console.log(x2Count);

console.log(x1["collection"]);
console.log(x2["collection"]);


[ vujkev @ 12.08.2012. 08:45 ] @
postoji li mogućnost da "collection" ne bude javno vidljiv?
Ne bih želeo da može direktno da se doda u "collection" sa "push", već da dodavanje mora da bude preko "add" funkcije.
[ Nikola Poša @ 12.08.2012. 17:36 ] @
Nevezano za tvoj post, preporučujem ti da baciš pogled na ova dva članka:

http://css.dzone.com/articles/javascript-does-not-need
http://css.dzone.com/articles/javascript-inheritance-example

Tu ćeš naći dosta korisnih stvari na temu izvodjenja u JavaScript-u, a i uopšte objektno-orijentisanog pristupa.
[ 357_97 @ 21.08.2012. 12:32 ] @
Citat:
vujkev:
postoji li mogućnost da "collection" ne bude javno vidljiv?

Mogu će je, ali tada moras da napravis privileged funkcije u Parent-u.
Code:
function Parent() {
    var _collection = _collection || [];

    this.add = function (i) {
        _collection.push(i);
    }

    this.getCollection = function () {
        return _collection;
    };

    this.getCount = function () {
        return _collection.length;
    }
}

function Child() {
    Parent.call(this);
}
 
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var x1 = new Child();
x1.add(1);
x1.add(3);
x1.add(5);
var x1Count = x1.getCount();

var x2 = new Child();
x2.add(2);

var x1Count = x1.getCount();
var x2Count = x2.getCount();