First off, I did search this in many places, but couldn't find an adequate answer, but I also realize this could simply be my failing.
This has to do with objects created by "new" vs Object.create()
Background: When Creating objects using "new", I get an object that is a copy of the original that is populated with the properties of the original, but is it's own thing. However, When I create an object using "Object.create()" I get a new object that seems to simply point to the prototype specified. This doesn't seem to be a problem when specifying values in the new object as the new values are created within the new object. But when I put key value pairs into an object within the object created by new it only affects the new object; however, if I do the same thing with the object created by Object.create() it changes the prototype and all objects sharing that prototype are affected.
Questions: Is this the correct behavior or am I doing something wrong? Intuitively, I expect any new objects created regardless of method to be separate "individuals" and separately changeable unless I explicitly change the prototype, but this doesn't seem to be what happens with Object.create()
How can I use Object.create() to create unique instances of the prototype and affect the objects within without affecting the prototype? Or should I just accept that this is not the behavior of Object.create() and use constructors instead?
Here's some code as an example:
function NewThing(){
this.testVal = 35;
this.testString = "Default";
this.testObj = {};
}
thing={
testVal: 35,
testString: "Default2",
testObj: {}
}
test1 = new NewThing()
//test1 becomes a new Object with all the properties of NewThing
test2 = Object.create(thing)
// test2 becomes an object that seems to point to the thing object
test3 = Object.create(thing)
// test3 becomes an object that also seems to point to the thing object
test1.testVal = 45
//testVal property of test1 seems changed fine
test2.testVal = 45
//testVal property of test2 seems changed and NOT test 3 which is good
test1.testObj["test"]="is fine"
//puts the pair in the object of test1
test2.testObj["test"]="is NOT fine"
//puts the pair in the PROTOTYPE affecting BOTH test2 and test3