[英]How can I store reference to a variable within an array?
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?有没有办法让数组引用变量?
Put an object into the array instead:将 object 放入数组中:
var name = {};
name.title = "foo";
var array = [];
array["reference"] = name;
name.title = "bar";
// now returns "bar"
array["reference"].title;
You can't.你不能。
JavaScript always pass by value . JavaScript总是按值传递。 And everything is an object;
一切都是object;
var
stores the pointer, hence it's pass by pointer's value . var
存储指针,因此它通过指针的值传递。
If your name = "bar"
is supposed to be inside a function, you'll need to pass in the whole array instead.如果您的
name = "bar"
应该在 function 内,则您需要传入整个数组。 The function will then need to change it using array["reference"] = "bar"
.然后 function 需要使用
array["reference"] = "bar"
更改它。
Btw, []
is an array literal.顺便说一句,
[]
是一个数组文字。 {}
is an object literal. {}
是 object 文字。 That array["reference"]
works because an Array is also an object, but array is meant to be accessed by 0-based index.该
array["reference"]
有效,因为 Array 也是一个 object,但该数组旨在通过从 0 开始的索引访问。 You probably want to use {}
instead.您可能想改用
{}
。
And foo["bar"]
is equivalent to foo.bar
.并且
foo["bar"]
等价于foo.bar
。 The longer syntax is more useful if the key can be dynamic, eg, foo[bar]
, not at all the same with foo.bar (or if you want to use a minimizer like Google's Closure Compiler).如果键可以是动态的,例如
foo[bar]
,与 foo.bar 完全不同(或者如果您想使用像 Google 的 Closure Compiler 这样的最小化器),更长的语法会更有用。
Try pushing an object to the array instead and altering values within it.尝试将 object 推送到数组并更改其中的值。
var ar = [];
var obj = {value: 10};
ar[ar.length] = obj;
obj.value = 12;
alert(ar[0].value);
My solution to saving a reference is to pass a function instead:我保存参考的解决方案是通过 function 代替:
If the variable you want to reference is called myTarget
, then use:如果您要引用的变量名为
myTarget
,则使用:
myRef = function (newVal) {
if (newVal != undefined) myTarget = newVal;
return myTarget;
}
To read the value, use myRef();
要读取值,请使用
myRef();
. . To set the value, use
myRef(<the value you want to set>);
要设置值,请使用
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.然后使用
myArray[0]()
读取并使用myArray[0](<new value>)
写入。
Disclaimer: I've only tested this with a numerical target as that is my use case.免责声明:我只用数字目标对此进行了测试,因为那是我的用例。
My solution to saving a reference is to pass a function instead:我保存参考的解决方案是通过 function 代替:
If the variable you want to reference is called 'myTarget', then use:如果您要引用的变量名为“myTarget”,则使用:
myRef = function (newVal) {
if (newVal != undefined)
myTarget = newVal;
return myTarget;
}
To read the value, use myRef();.要读取该值,请使用 myRef();。 To set the value, use myRef(value_to_set);.
要设置值,请使用 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.然后使用 myArray0 读取并使用
myArray[0](value_to_set)
写入。
Disclaimer: I've only tested this with a numerical target as that is my use case.免责声明:我只用数字目标对此进行了测试,因为那是我的用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.