0

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 
Newb1
  • 103
  • 4
  • "*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*" - well, no. The original does not have any properties. The constructor is a *function* that *runs* to initialise the object, and *creates* those properties exclusively on the instance. You can do the same with a normal function after creating objects using `Object.create`. – Bergi Nov 29 '15 at 14:39
  • Not sure whether a duplicate or only helpful, but have a look at [Benefits of using `Object.create` for inheritance](http://stackoverflow.com/a/17393153/1048572) and [Prototypal inheritance - Issues with nested objects](http://stackoverflow.com/q/10131052/1048572) – Bergi Nov 29 '15 at 14:41
  • @Bergi Thanks for the correction. You are correct. However, I still need to understand what's going on with the objects in the new objects created with the different methods. Thanks for the links. I'll read them shortly. – Newb1 Nov 29 '15 at 14:49

1 Answers1

0

NewThing creates new object for testObj on each invocation.

With Object.create, since you are not assigning to testObj, then you are changing a shared object referenced by testObj.

It is as if you used a shared object in the NewThing:

sharedObj = {};
function NewThing(){
  this.testVal = 35;
  this.testString = "Default";
  this.testObj = sharedObj;
}
paiv
  • 5,491
  • 22
  • 30