如何在JavaScript中通过引用传递变量?

我有三个变量,我想对它们执行几个操作,所以我想把它们放在for循环中,并对每个变量执行操作.

伪代码:

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中没有可用的"通过引用传递".可以传递对象(也就是说,可以通过值传递对对象的引用),然后让函数修改对象内容:

function alterObject(obj) {
  obj.foo = "goodbye";
}

var myObj = { foo: "hello world" };

alterObject(myObj);

alert(myObj.foo); // "goodbye" instead of "hello world"

如果需要,可以使用数字索引迭代数组的属性并修改数组的每个单元格.

var arr = [1, 2, 3];

for (var i = 0; i < arr.length; i++) { 
    arr[i] = arr[i] + 1; 
}

需要注意的是,"通过引用传递"是一个非常具体的术语.这不仅仅意味着可以将引用传递给可修改的对象.相反,这意味着可以以允许函数在calling上下文中修改该值的方式传递简单变量.所以:

 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++这样语言中,这样做是可能的,因为语言does(排序)具有按引用传递.

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.

edithere 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相关问答推荐

我可以在useState中调用函数并使用其数据吗

使用事件监听器在JavaScript中重新分配全局变量

错误:(0,react__WEBPACK_IMPORTED_MODULE_1__.useSYS State)不是函数或其返回值不可迭代

Express.js:以块形式发送响应

Angular 拦截器错误处理删除方法问题

通过实现regex逻辑自定义数据表搜索

JavaScript文本区域阻止KeyDown/KeyUp事件本身上的Alt GR +键组合

使用useup时,React-Redux无法找到Redux上下文值

我应该绑定不影响状态的函数吗?'

将现场录音发送到后端

未捕获错误:[]:getActivePinia()被调用,但没有活动Pinia.🍍""在调用app.use(pinia)之前,您是否try 使用store ?""

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

如何通过将实例方法的名称传递给具有正确类型的参数的继承方法来调用实例方法?

Next.js服务器端组件请求,如何发送我的cookie token?

为什么123[';toString';].long返回1?

如何将innerHTML字符串修剪为其中的特定元素?

从页面到应用程序(NextJS):REST.STATUS不是一个函数

以编程方式聚焦的链接将被聚焦,但样式不适用

如何使用Astro优化大图像?