Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

javascript - 在Javascript中按引用传递变量(Pass Variables by Reference in Javascript)

How do I pass variables by reference in JavaScript?(如何在JavaScript中通过引用传递变量?)

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.(我有3个变量,我想对它们执行几个操作,因此我想将它们放在for循环中并对每个变量执行操作。) pseudo code:(伪代码:) 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 What is the best way to do this?(做这个的最好方式是什么?)   ask by BFTrick translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no "pass by reference" available in JavaScript.(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" In a language like C++, it's possible to do that because that language does (sort-of) have pass-by-reference.(在像C ++这样的语言中,这样做是有可能的,因为该语言确实具有(通过)引用传递。) 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.(编辑 -最近(2015年3月)在Reddit上再次通过类似于下面提到的我的博客文章引起轰动,尽管在本例中是关于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".(我在阅读Reddit评论中来回时想到,其中很大一部分混乱源于涉及“参考”一词的不幸碰撞。) 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.(特别要注意的是,在一种真正的传递引用语言(一种确实涉及对象)中,一种仍将具有修改对象内容的能力,并且看起来与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.(但是, 可以在调用环境中修改对象引用,这是您无法在JavaScript中完成的关键操作。) A pass-by-reference language would pass not the reference itself, but a reference to the reference .(引用传递语言不会传递引用本身,而是传递对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.)((请注意该帖子的注释,该注释解释了C ++确实没有传递引用。的确如此。但是,C ++的功能是能够创建对纯变量的引用,无论是在函数点处显式创建调用以创建指针,或在调用参数类型签名要求完成的函数时隐式调用。这是JavaScript不支持的关键。))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...