Lets say I have this:
function Foo(){
this.name = "fooos";
};
Now later in the script I have a reference to Foo, and what to add properties to Foo when instantiated. I could do something like this:
Foo.prototype.myProp = 3;
foo = new Foo();
expect(foo).to.have.property('name');
expect(foo).to.have.property('myProp');
This works great until I need to attach an object to prototype, like so:
Foo.prototype.myProp = { bar : 3 };
The issue is that now instances will share state on bar (as `bar is just a reference):
foo = new Foo();
foo2 = new Foo();
foo.myProp.bar = 5;
expect(foo2.myProp.bar).to.equal(5);
Which is not desirable. However if the object is added inside the constructor the instances do not share state.
Is there a way around this? I don't have access to the constructor to manipulate it. I need to attach an object to an instance from the prototype.