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

2
  • Object.assign() helpful ? Commented Apr 27, 2017 at 17:33
  • This is not possible. There are no references to other variables in JavaScript. Use a getter function or property instead. Commented Apr 27, 2017 at 18:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.