Problem with inheritance, Object.Create in Javascript?

I think you are a bit confused here options: { name: 'foo' }, name: 'foo as you defined them, and then used in your Object.create() statement, belong to the prototype and as such are the same for all objects with the same prototype The reason you see name attribute changed is because you created it on each instance (essentially overriding that defined in the prototype) by calling: this. Name = name Something that doesn't happen for the object property because you never assigned it directly. Instead it takes the last assigned value (which would be 2 in your example) Had you written: this.

Options = {}; this.options. Name = name in your init function - options. Name would be overridden on each instance and be just like you wanted it.

I think you are a bit confused here. Options: { name: 'foo' }, name: 'foo' as you defined them, and then used in your Object.create() statement, belong to the prototype and as such are the same for all objects with the same prototype. The reason you see name attribute changed is because you created it on each instance (essentially overriding that defined in the prototype) by calling: this.Name = name; Something that doesn't happen for the object property because you never assigned it directly.

Instead it takes the last assigned value (which would be 2 in your example). Had you written: this. Options = {}; this.options.

Name = name; in your init function - options. Name would be overridden on each instance and be just like you wanted it.

It looks like something to do with passing values by reference. The object Test contains a reference to another object in property options. When a new instance of Test is created the new instance get a reference to the existing options instance instead of getting a new copy of the object.

A rough image will be something like You can check it by adding the following statement at the bottom of your code alert(Test. Options== dict0. Options).

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).

I think that doesn't answer the question why name varies for all instances and options. Name doesn't. But... may be I am mistaken.

Will provide my answer anyway. – ZenMaster Sep 14 at 12:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions