11

I'm trying to create an array that maps strings to variables. It seems that the array stores the current value of the variable instead of storing a reference to the variable.

var name = "foo";
var array = [];

array["reference"] = name;

name = "bar";

// Still returns "foo" when I'd like it to return "bar."
array["reference"];

Is there a way to make the array refer to the variable?

0

5 Answers 5

15

Put an object into the array instead:

var name = {};
name.title = "foo";

var array = [];

array["reference"] = name;

name.title = "bar";

// now returns "bar"
array["reference"].title;
Sign up to request clarification or add additional context in comments.

Comments

10

You can't.

JavaScript always pass by value. And everything is an object; var stores the pointer, hence it's pass by pointer's value.

If your name = "bar" is supposed to be inside a function, you'll need to pass in the whole array instead. The function will then need to change it using array["reference"] = "bar".

Btw, [] is an array literal. {} is an object literal. That array["reference"] works because an Array is also an object, but array is meant to be accessed by 0-based index. You probably want to use {} instead.

And foo["bar"] is equivalent to foo.bar. The longer syntax is more useful if the key can be dynamic, e.g., foo[bar], not at all the same with foo.bar (or if you want to use a minimizer like Google's Closure Compiler).

11 Comments

This is the answer I was looking for. I realize objects can be used for a similar effect, but this answer gets at the heart of the issue.
Javascript doesn't always pass by value...for example. if you have abc = {a:2}; and then ddd=abc;. You don't have two objects. ddd keeps a reference to abc. And if you change something on ddd it will change on abc. Which is not pass by value means.
This should not be the accepted answer. You can store a reference to an object in javaScript like Muhammad Umer said.
@RichN I wasn't referring to OP's case, my example had nothing to do with OP - It was to demonstrate that two variables may refer to the same object within JS which is only possible when passing by reference. That linked stack overflow post is also just plain wrong: They're the ones playing a silly semantics game. Passing by reference means passing a pointer. Saying that it passes the Value of a pointer is just an incoherent word game. Reference = Pass by pointer. If you don't believe that then pass by reference has no meaning whatsoever in programming. C and C++ were my first languages...
It really boggles my mind anyone would legitimately argue that C doesn't have pass by reference - Its incoherent. PassByReference was a term invented to describe passing a variables memory location as a variable rather than the data value itself. In C you designate a variable as either being a pointer or not with a *, when you pass a pointer instead of a regular data variable that is pass-by-reference, a pointer is a reference to the object of interest. Thats the only useful meaning of the term pass-by-reference. It is technically just a "Value" still, but so is every var in programmin ever
|
5

Try pushing an object to the array instead and altering values within it.

var ar = [];

var obj = {value: 10};
ar[ar.length] = obj;

obj.value = 12;

alert(ar[0].value);

Comments

2

My solution to saving a reference is to pass a function instead:

If the variable you want to reference is called myTarget, then use:

myRef = function (newVal) {
    if (newVal != undefined) myTarget = newVal;
    return myTarget;
}

To read the value, use myRef();. To set the value, use myRef(<the value you want to set>);.

Helpfully, you can also assign this to an array element as well:

var myArray = [myRef];

Then use myArray[0]() to read and myArray[0](<new value>) to write.

Disclaimer: I've only tested this with a numerical target as that is my use case.

Comments

0

My solution to saving a reference is to pass a function instead:

If the variable you want to reference is called 'myTarget', then use:

myRef = function (newVal) {
            if (newVal != undefined)
                myTarget = newVal;
            return myTarget;
        }

To read the value, use myRef();. To set the value, use myRef(value_to_set);.

Helpfully, you can also assign this to an array element as well:

var myArray = [myRef];

Then use myArray0 to read and myArray[0](value_to_set) to write.

Disclaimer: I've only tested this with a numerical target as that is my use case.

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.