The documentation is clear:
Creates a new object with the specified prototype object and properties.
So Test
will be the prototype of the new object. All instances reference the same prototype and as options
is an object, they also share a reference to this one.
The relation looks like this:
+------------+ +---------+ +--------+
| Instance 1 |----->| Test | | Object |
+------------+ | | | |
| options-|------>| name |
| name | +--------+
+---------+
^
+------------+ |
| Instance 2 |-----------+
+------------+
Now, as options
is an object, if you assign a new property to it, like
instance1.object.name2 = 'bar';
you are actually accessing Test.options
. The result will be:
+------------+ +---------+ +--------+
| Instance 1 |----->| Test | | Object |
+------------+ | | | |
| options-|------>| name |
| name | | name2 |
+---------+ +--------+
^
+------------+ |
| Instance 2 |-----------+
+------------+
But when you assign a new value to name
, a new property name
will be created at that instance. So when you do:
instance1.name = 'bar';
the result will be
+------------+ +---------+ +--------+
| Instance 1 |----->| Test | | Object |
| | | | | |
| name | | options-+------>| name |
+------------+ | name | +--------+
+---------+
^
+------------+ |
| Instance 2 |-----------+
+------------+
The same would happen if, instead of just accessing or assigning a property of/to options
, you assigned a new value to it:
instance1.options = {name: 'another name'};
results in:
+--------+
| Object |
| |
| name |
+--------+
^
+------------+ | +---------+ +--------+
| Instance 1 |--+-->| Test | | Object |
| | | | | | |
| options----+--+ | options-|------>| name |
+------------+ | name | +--------+
+---------+
^
+------------+ |
| Instance 2 |-----------+
+------------+
Because of how property look up works, instance1.options
will return the value of the closest (nearest) options
property in the prototype chain. Once we set name
or options
at the instance, it will return those values and not the ones of the prototype (Test
).