arrays - JavaScript object stores previous values even in new object -




javascript behaves weirdly in case of objects. although i'm not sure if correct behaviour.

inside new object(), set properties in object. next time when again new object(), instead of default values values set in previous instance. argh.

below example explains problem clearly

function testo() {}  testo.prototype = {   obj: {     what: {       value: 5     }   },   done: function () {     console.log(this.obj.what.value);     this.obj.what = {value: 10};   } };  var x = new testo(); x.done(); var y = new testo(); y.done(); 

the output of above code is:-

5 10 

i expecting be:-

5 5 

why? because i'm creating new class() , in previous instance had set value using 'this', not static , default properties of objects inside should show up.

i have created above example demo. i'm facing issue in library. know has objects stored reference.

how should proceed expected output? thoughts?

you move prototype property (which instances identical) object in class.

function testo() {      this.obj = { what: { value: 5 } };  }    testo.prototype = {      done: function () {          console.log(this.obj.what.value); this.obj.what = { value: 10 };      }  };    var x = new testo();  x.done();    var y = new testo();  y.done();





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -