Skip to content Skip to sidebar Skip to footer

Object References To Undefined In Javascript

If you have an object in javascript and it references another object and then 2nd object changes, you can expect to see the change in the referenced object. However, if the 2nd obj

Solution 1:

The answer is pretty simple. You need to understand the difference between a value and reference type variables.

In the first example have a reference type variable i.e. myObj which points to an area in the memory. Then you define one more variable i.e. pointer that points to the same area. That's why any changes to one object is instantly reflected on the other.

In the second example things are slightly different. You define the myObj which is of Undefined type. Which means that there is no value specified for this variable. You would do that in the case where you have a value type variable e.g. Number, string etc. for which you don't have a value to assign; It also means that myObj doesn't point to any are in the heap section of the memory. Then when you do this var pointer = myobj; you're simply copying the value of one variable to the other; It's similar to doing this, for example: var pointer = 5; This is called copy by value' so that's why the changes are not reflected.

Post a Comment for "Object References To Undefined In Javascript"