问题描述
如何在 JavaScript 中通过引用传递变量?
How do I pass variables by reference in JavaScript?
我有三个变量要对其执行多个操作,因此我想将它们放在一个 for 循环中并对每个变量执行操作.
I have three variables that I want to perform several operations to, so I want to put them in a for loop and perform the operations to each one.
伪代码:
myArray = new Array(var1, var2, var3);
for (var x = 0; x < myArray.length; x++){
// Do stuff to the array
makePretty(myArray[x]);
}
// Now do stuff to the updated variables
最好的方法是什么?
推荐答案
JavaScript 中没有可用的通过引用传递".您可以传递一个对象(也就是说,您可以通过值传递对对象的引用),然后让函数修改对象内容:
There is no "pass by reference" available in JavaScript. You can pass an object (which is to say, you can pass-by-value a reference to an object) and then have a function modify the object contents:
function alterObject(obj) {
obj.foo = "goodbye";
}
var myObj = { foo: "hello world" };
alterObject(myObj);
alert(myObj.foo); // "goodbye" instead of "hello world"
如果需要,您可以使用数字索引迭代数组的属性并修改数组的每个单元格.
You can iterate over the properties of an array with a numeric index and modify each cell of the array, if you want.
var arr = [1, 2, 3];
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i] + 1;
}
需要注意的是,传递引用"是一个非常特殊的术语.这不仅仅意味着可以传递对可修改对象的引用.相反,这意味着可以以允许函数在调用上下文中修改该值的方式传递一个简单变量.所以:
It's important to note that "pass-by-reference" is a very specific term. It does not mean simply that it's possible to pass a reference to a modifiable object. Instead, it means that it's possible to pass a simple variable in such a way as to allow a function to modify that value in the calling context. So:
function swap(a, b) {
var tmp = a;
a = b;
b = tmp; //assign tmp to b
}
var x = 1, y = 2;
swap(x, y);
alert("x is " + x + ", y is " + y); // "x is 1, y is 2"
在像 C++ 这样的语言中,可以做到这一点,因为该语言确实(排序)具有传递引用.
In a language like C++, it's possible to do that because that language does (sort-of) have pass-by-reference.
编辑 —最近(2015 年 3 月)在 Reddit 上通过一篇类似于我下面提到的博客文章再次引起轰动,尽管在这种情况下是关于 Java.我在阅读 Reddit 评论中的来回评论时突然想到,很大一部分混乱源于涉及引用"一词的不幸冲突.术语通过引用传递"和通过值传递"早于在编程语言中使用对象"的概念.它真的完全不是关于对象的.它是关于函数参数,特别是函数参数如何连接"(或不连接)到调用环境.特别要注意的是,在真正的传递引用语言中—一个确实涉及对象—一个人仍然可以修改对象内容,而且它看起来与在 JavaScript 中几乎一模一样.但是,还能够在调用环境中修改对象引用,而这是您在 JavaScript 中不能做的关键事情.传递引用语言不会传递引用本身,而是对引用的引用.
edit — this recently (March 2015) blew up on Reddit again over a blog post similar to mine mentioned below, though in this case about Java. It occurred to me while reading the back-and-forth in the Reddit comments that a big part of the confusion stems from the unfortunate collision involving the word "reference". The terminology "pass by reference" and "pass by value" predates the concept of having "objects" to work with in programming languages. It's really not about objects at all; it's about function parameters, and specifically how function parameters are "connected" (or not) to the calling environment. In particular, note that in a true pass-by-reference language — one that does involve objects — one would still have the ability to modify object contents, and it would look pretty much exactly like it does in JavaScript. However, one would also be able to modify the object reference in the calling environment, and that's the key thing that you can't do in JavaScript. A pass-by-reference language would pass not the reference itself, but a reference to the reference.
编辑 —这里有一篇关于该主题的博文.(请注意该帖子的评论,该评论解释了 C++ 并没有真正通过引用传递.这是真的.然而,C++ 确实具有创建对普通变量的引用的能力,或者在该点显式地创建引用函数调用来创建一个指针,或者隐式在调用其参数类型签名要求完成的函数时.这些是 JavaScript 不支持的关键.)
edit — here is a blog post on the topic. (Note the comment to that post that explains that C++ doesn't really have pass-by-reference. That is true. What C++ does have, however, is the ability to create references to plain variables, either explicitly at the point of function invocation to create a pointer, or implicitly when calling functions whose argument type signature calls for that to be done. Those are the key things JavaScript doesn't support.)
这篇关于在 JavaScript 中通过引用传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!