2

suppose i have this

var x={};    //x is an address in the memory where object is stored
var z=x;     //z=x after execution is equal to z={} right?

now z has nothing to do with x or not related to x after execution so when,

x={name:"Maizere"};
z!=x        //true

but, when

x.name="maizere";
alert(z.name)//maizere why?

we are not setting the value of z but x and z relation to x shouldn't exit anymore

actual code:

 x={};
 y=x;
 x.name="maizere";
 alert(y.name)//maizere

I really have no knowledge of how this is working .Can anyone explain this in detail please?

3
  • 2
    alert(z.name) -> undefined ... you missed something. Commented Jan 31, 2013 at 17:45
  • 2
    I'm getting undefined. Please show us your exact code, and use comments properly. Commented Jan 31, 2013 at 17:45
  • @Bergi no u did some mistake ,u get "Maizere" .Check the code u tested plz Commented Jan 31, 2013 at 17:50

2 Answers 2

3

Your initial assumption is wrong; z is a pointer to the same object as x.

var x = {}; 
var z = x;

alert( z === x );    // true

When you do x = { name: "Maizere" }; you're assigning a new object to x. z is still pointing to the original object.

x = { name: "Maizere" };
alert( z !== x );    // true

In the latter example you're not creating a new object but changing the original object's property.

var x = {}; 
var z = x;

x.name = "maizere";
alert( z === x );    // true

A further example of where the confusion might stem from: the bracket syntax creates a new object instead of modifying the original.

var x = { name: "Maizere" };
var y = { name: "Zaimere" };

x = { age: 20 };
y.age = 30;

console.log( x );  // {age: 20}                  <-- original object is replaced
console.log( y );  // {name: "Zaimere", age: 30} <-- original object is modified
Sign up to request clarification or add additional context in comments.

2 Comments

according to u if x={} and z={} x==z //true so if x.name="maizere" will z have the property "name"
No. The objects are equal, but not the same object. If you change one of them they're not equal anymore. (Just like if you have x=1; z=1; x==z // true but if you change z=2 then x != z.)
3

After these two statements:

x={};
y=x;

The internal representation is like this:

      +---- x
      |
{} <--+
      |
      +---- y

So any changes to x are reflected in y:

 x.name="maizere";
 alert(y.name)//maizere

Updated:

                     +---- x
                     |
{name: 'maizere'} <--+
                     |
                     +---- y

This goes away once you assign either variable to something else:

x = { name: "Maizere" }

Representation:

{name: 'Maizere'} <------- x

{name: 'maizere'} <------- y

3 Comments

but what if i do x = { name: "Maizere" }; after z value is set
That creates a new object and changes x to point to it, it has no effect on z.
@MaizerePathak I've updated the answer with what I think you meant with the comment, let me know if that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.