1

I wonder if this was possible:

// Suppose i have two object.
var a = {sample:1};
var b = {sample:9};

var ra = a; // Referenced
var rb = b; // Referenced

var rc = ra; // Referenced rc.

console.log(rc); // Output {test:1} => correct
ra = b; // Changed Reference of ra.
console.log(rc); // Output {test:1} => [...?]

// Logically rc output is correct since it retained the reference to object a
// But what I want is object b instead

// That is when: var rc = ra; its reference should point to variable ra, not to variable a;

var rd = ra = a; // Reset
ra = b;
console.log(ra); // {sample:9}
console.log(rd); // {sample:1} => but want{sample:9}

Any idea on how to do this would be greatly appreciated :)

Noobit
  • 411
  • 3
  • 8

1 Answers1

0

The first two lines creates two objects and assign their references to the variables a and b:

                                   ---------------
a -------------------------------> | {sample: 1} |
                                   ---------------
b -------------------------------> | {sample: 9} |
                                   ---------------

The line ra = a assignes whatever reference stored in a to ra so now ra and a points to the same object, the same goes for rb = b:

    /--------------------------\
    |                          |   ---------------
    |  a ----------------------\-> | {sample: 1} |
    |                              ---------------  
    |  b ------------------------> | {sample: 9} |<-\
    |                              ---------------  |
    |                                               |
ra -/                                               |
                                                    |
rb -------------------------------------------------/

rc = ra means that rc is pointing to whatever ra is pointing to:

    /--------------------------\
    |                          |   ---------------
    |  a ----------------------\-> | {sample: 1} |<----\
    |                              ---------------     |
    |  b ------------------------> | {sample: 9} |<-\  |
    |                              ---------------  |  |
    |                                               |  |
ra -/                                               |  |
                                                    |  |
rb -------------------------------------------------/  |
                                                       |
rc ----------------------------------------------------/

ra = b means that ra is now pointing to the object pointed to by b:

                                   ---------------
       a ------------------------> | {sample: 1} |<----\
                                   ---------------     |
       b ----------------------/-> | {sample: 9} |<-\  |
                               |   ---------------  |  |
                               |                    |  |
ra ----------------------------/                    |  |
                                                    |  |
rb -------------------------------------------------/  |
                                                       |
rc ----------------------------------------------------/

Now when logging rc, we find out that rc is pointing to {sample: 1} not to {sample: 9}.

So it's not possible, when assigning a reference of an object to a variable, only that variable changes (point to the object), the other variables that points to the same object remain intact.

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73