1

Is there any way for a variable to act as a reference to another variable? Could I have multiples variable all hold the same information regarding a single piece of data?

For example:

var foo = "data";
var bar = foo;

// I want this to set both foo & bar to null
foo = null;

console.log(bar); // However, bar is still set to "data"

In C or C++ you're able to get this desired behavior with pointers; having two variables reference the same location in memory.

Is there some way to mimic this behavior in JavaScript?

4
  • I don't think you get your desired behavior in C/C++ either. If you set pointer b to some non-null pointer a and then set a to null, pointer b will still point to the non-null place in memory. (That's not to say that you can't get the desired behavior in C++ with pointer references.) Commented Dec 17, 2015 at 21:19
  • @TedHopp What I meant was if foo was an Obj* and bar was also an Obj* and they both pointed to the same Obj, if foo or bar changed their Obj value then the update would appear to both variables. Commented Dec 17, 2015 at 21:22
  • Well, yes. That happens in JavaScript as well. However, the code you posted would do nothing like that in C (if you changed var to char *). Commented Dec 17, 2015 at 21:23
  • @TedHopp Correct, hence my question Commented Dec 17, 2015 at 21:24

4 Answers 4

4

In JavaScript, variables that have objects as their value are actually scalar references (pointers) to objects, so assignment simply copies object references, not the objects themselves. So when you do

var bar = foo;

the value of foo gets copied. This means both foo and bar have copies of the same value.

Even with objects, this won't work

var x = {};
var y = x; // y and x both point to the same object
x = null;
// y still points to the object

Again, x and y are copies of the reference, so both x and y are separate references, each with the same value. But nulling one out does nothing to do the other.

The closest you can get is what @Derek朕會功夫 said, which is to do

var x = {val: 'data'};
var y = x;
delete x.val;
// y.val is now undefined 

And pass x or y around.

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

6 Comments

+1 for a substantially correct answer, but pass-by-value semantics has nothing to do with this, since nothing is being passed (no function calls are involved).
Ah okay; it's too bad JavaScript doesn't have an easy way to do this. Thanks for the clarification!
@TedHopp ah yes, I see. Is there a special name for reference values being copied on assignment?
I'm not aware of any special name for the assignment of reference values—or for assignment of any other scalar value, for that matter,
@TedHopp hmm what should the answer read then? "uses pass by value semantics when assigning variable values"?
|
2

Yes. Sorta. Object references are similar to pointers in C:

var foo = {value: "data"};
var bar = foo;

foo.value = undefined;
console.log("Value: %s", bar.value);  // undefined

Comments

1

No you cannot. If the value is a primitive, the value is copied, and if it's an object the reference is copied.

The only thing you can do is point multiple variables to the same object. However reassigning either variable will not re-assign the other.

Comments

0

It happens because of different variables types in JavaScript: primitive and reference. Have a look at this article, section Primitive Types and Reference Types explains it really well https://www.safaribooksonline.com/library/view/javascript-the-definitive/0596101996/ch04.html

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.