javascript 如何在数组中存储对变量的引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5865094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I store reference to a variable within an array?
提问by Steve Geluso
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?
有没有办法让数组引用变量?
采纳答案by RichN
You can't.
Javascript alwayspass by value. And everything is an object; var
stores the pointer, hence it's pass by pointer's value.
你不能。
Javascript总是按值传递。一切都是对象;var
存储指针,因此它通过指针的值传递。
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"
.
如果您name = "bar"
应该在函数内部,则需要传入整个数组。然后该函数需要使用 来更改它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.
顺便说一句,[]
是一个数组文字。{}
是一个对象字面量。这是array["reference"]
有效的,因为 Array 也是一个对象,但数组旨在通过基于 0 的索引访问。您可能想{}
改用。
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).
并且foo["bar"]
等价于foo.bar
。如果键可以是动态的,则更长的语法更有用,例如,foo[bar]
与 foo.bar 完全不同(或者如果您想使用像 Google 的 Closure Compiler 这样的最小化器)。
回答by Lars Blumberg
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;
回答by Marty
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);
回答by yatt
I know this is an old thread, but it came up when I was googling the same issue. My solution to saving a reference is to pass a function instead:
我知道这是一个旧线程,但是当我在谷歌上搜索同样的问题时它出现了。我保存引用的解决方案是传递一个函数:
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();
To set the value, use myRef(<the value you want to set>);
要读取值,请使用myRef();
要设置值,请使用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.
有用的是,您也可以将其分配给数组元素。
var myArray = [myRef];
然后用于myArray[0]()
读取和myArray[0](<new value>)
写入。
Disclaimer:I've only tested this with a numerical target as that is my use case.
免责声明:我仅使用数字目标对此进行了测试,因为这是我的用例。
回答by yatt
CORRECTED version (I can't see how to edit my last post):
更正版本(我看不到如何编辑我的上一篇文章):
I know this is an old thread, but it came up when I was googling the same issue. My solution to saving a reference is to pass a function instead:
我知道这是一个旧线程,但是当我在谷歌上搜索同样的问题时它出现了。我保存引用的解决方案是传递一个函数:
if the variable you want to reference is called 'myTarget', then use:
如果要引用的变量名为“myTarget”,则使用:
myRef = function (newVal) { if (newVal != undefined) myTarget = newVal; return myTarget; }
myRef = function (newVal) { if (newVal != undefined) myTarget = newVal; 返回我的目标;}
To read the value, use myRef(); To set the value, use myRef(value_to_set);
要读取值,请使用 myRef(); 要设置值,请使用 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 myArray0 to write.
有用的是,您也可以将其分配给数组元素。var myArray = [myRef]; 然后使用 myArray0 读取和 myArray0 写入。
Disclaimer: I've only tested this with a numerical target as that is my use case.
免责声明:我仅使用数字目标对此进行了测试,因为这是我的用例。