0

Look my problem:

I have a class that look like this:

var els=[];
var base = function(){
    this.config = {}
}


var X1 = function(){
}
X1.prototype = new base();
X1.prototype.indexme = function(i){
    this.config.index = i;
}
X1.prototype.add = function(){
    var i = els.push(this)
    this.indexme(i)
}

var teste = new X1();
teste.add();
var teste2 = new X1();
teste2.add();
var teste3 = new X1();
teste3.add();

console.log(els)

Why this.config.index is overwritten to another instances?

I expected that teste have config.index = 1; teste2 config.index= 2 and teste3 config.index=3

Thanks

Bruno Casado
  • 63
  • 1
  • 11
  • What is `this.teste(i)`? Also, you're inheriting the whole `config` object. It's always the same object. – Amadan Sep 16 '14 at 01:39
  • You're pushing the X1 Object onto els, so the index won't be be a number. – StackSlave Sep 16 '14 at 01:44
  • I think `X1.prototype.test` should be `X1.prototype.teste` = bad edit – StackSlave Sep 16 '14 at 01:48
  • 1
    Why do you guys downvote? I don't think this is a naive question. This.config references to the X1.prototype.config, and all instances of X1 will share the same prototype, so teste1, teste2 and teste3 will get the same result for config.index – wander Sep 16 '14 at 01:49
  • @wander i dont know too. I have a doubts and i lost a day to solve my problem. I think that when you inherit to a class, the base properties should be a model to another classes but no references. Each class it should be a new parent instance. – Bruno Casado Sep 16 '14 at 02:03
  • 1
    Prototype is attached to the class definition, so you can treat prototype as a static property of class. Then clearly, every instance of a certain class will share the same prototype. – wander Sep 16 '14 at 02:08
  • 1
    Here is a better way to setup inheritance: http://stackoverflow.com/q/17392857/218196 – Felix Kling Sep 16 '14 at 02:40
  • @FelixKling for me this make more sense. Dog.prototype = Object.create(Animal.prototype); This approach do exactly what i want – Bruno Casado Sep 16 '14 at 12:32

1 Answers1

2

All instances of X1 share the same prototype, which is an instance of base with a config property. Thus, all instances of X1 share the same config property. You can move the line this.config = {}; to the X1 constructor or you can define an init() function in base that assigns this.config for each object and call init() from the X1 constructor.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • The idea is only the Base be a model to my another classes, if i do a copy of this.config in init of another classes is a bad solution? – Bruno Casado Sep 16 '14 at 02:09
  • More one doubt. If i do : this.config = {index: var} all of my instances get the correct values. Why this.config.index get the same values? In theory this.config already have references in the prototype? Or the simple fact that i do an attribution clean the references? – Bruno Casado Sep 16 '14 at 02:12
  • 1
    When you assign a `config` property to `this`, it hides the same property in the prototype, but the prototype property is still there. – Ted Hopp Sep 16 '14 at 04:13