在Javascript中通过引用传递变量 [英] Pass Variables by Reference in Javascript

查看:129
本文介绍了在Javascript中通过引用传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JS中通过引用传递变量?我有3个变量,我想要执行几个操作,所以我想把它们放在for循环中并对每个变量执行操作。

How do I pass variables by reference in JS? I have 3 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 vars

什么是最好的方法吗?

推荐答案

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"

现在,在你的情况下,你不是据我所知,无论如何都要传递任何东西。您可以使用数字索引迭代数组的属性,并根据需要修改数组的每个单元格。

Now, in your case, you're not passing anything anyway, as far as I can tell. You can iterate over the properties of an array with a numeric index and modify each cell of the array, if you want.

重要的是要注意传递 - 参考是一个非常具体的术语。它并不仅仅意味着可以将引用传递给可修改的对象。相反,它意味着可以传递一个简单的变量,以允许函数在调用上下文中修改该值。所以:

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中通过引用传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆