Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Nasleđivanje u javascriptu

[es] :: Javascript i AJAX :: Nasleđivanje u javascriptu

[ Pregleda: 2580 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

vujkev
Beograd

Član broj: 8072
Poruke: 1347
*.dynamic.isp.telekom.rs.



+104 Profil

icon Nasleđivanje u javascriptu11.08.2012. u 23:44 - pre 142 meseci
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


Naučio sam...
Da je važnije biti ljubazan nego biti u pravu
 
Odgovor na temu

574nk3
Software Developer
Freelance
Belgrade

Član broj: 38673
Poruke: 248
*.dynamic.isp.telekom.rs.



+33 Profil

icon Re: Nasleđivanje u javascriptu12.08.2012. u 08:29 - pre 142 meseci
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"]);


 
Odgovor na temu

vujkev
Beograd

Član broj: 8072
Poruke: 1347
*.dynamic.isp.telekom.rs.



+104 Profil

icon Re: Nasleđivanje u javascriptu12.08.2012. u 08:45 - pre 142 meseci
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.
Naučio sam...
Da je važnije biti ljubazan nego biti u pravu
 
Odgovor na temu

Nikola Poša
Backend (PHP) developer
Beograd

Član broj: 173839
Poruke: 1616
*.adsl-a-5.sezampro.rs.



+33 Profil

icon Re: Nasleđivanje u javascriptu12.08.2012. u 17:36 - pre 142 meseci
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.
 
Odgovor na temu

357_97

Član broj: 53056
Poruke: 104
*.dynamic.sbb.rs.



+5 Profil

icon Re: Nasleđivanje u javascriptu21.08.2012. u 12:32 - pre 141 meseci
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();
 
Odgovor na temu

[es] :: Javascript i AJAX :: Nasleđivanje u javascriptu

[ Pregleda: 2580 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.