What you are doing here is generally considered bad practice. You don't want to overwrite the prototype, but you want to add to it.
Now to core of your problem. You are defining stuff
as an attribute to the prototype. That means any object that is created using that prototype will share that object. If you want each instance to have it's own stuff
, it needs to be an instance variable:
function myclass(){
this.stuff = {};
}
myclass.prototype = {
getStuff: function(){
return Object.keys(this.stuff).length;
}
};
But like I said, don't redefine the prototype, add onto it:
function myclass(){
this.stuff = {};
}
myclass.prototype.getStuff = function(){
return Object.keys(this.stuff).length;
}
Now, anything added to stuff
will only be added to that particular instance:
var foo = new myclass();
foo.stuff.thing = "Hey, I'm foo";
var bar = new myclass();
bar.stuff.thing = "Hey, I'm bar";
console.log(bar.stuff); //{thing: "Hey, I'm bar"}