简体   繁体   English

如何在数组中存储对变量的引用?

[英]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.

相关问题 如何在javascript中存储对变量的引用? - How can I store a reference to a variable in javascript? 如何在变量中存储包含哈希标记的完整URL? - How can I store a full url that contains a hashmark within a variable? 如何在一个promise数组中引用一个对象值? - How can I reference an object value within an array of promises? 在 JavaScript 中,如何将对象值数组存储到字符串变量中? - In JavaScript, how can I store an array of object value into string variable? 如何在jQuery中存储引用表? - How can I store a reference table in jQuery? 我想知道的宽度 <div> 在我的指令模板中,并存储在变量中我该如何实现 - i want to know the width of the <div> within my directives template ,and store in the variable how can i achieve that 如何在“this”方法中引用“this”属性? - How can I reference a `this` property within a method of `this`? 在Javascript中,如何从该函数中调用但在其他位置定义的函数中引用函数范围的变量? - In Javascript, how can I reference a function-scoped variable from a function called within that function but defined elsewhere? 为什么我不能在自定义对象中引用变量? - Why can't I reference a variable within a custom object? 如何根据某些条件在数组内的对象中增加变量? - How can I increment a variable within an object that is within an array based on certain conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM