javascript javascript传递对象作为参考
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16880418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript pass object as reference
提问by Basit
i have a object, which is getting passed in many different functions inside a function. these functions may or may not change the value of the object, but if they do change it, then i would like to get the latest changes on object.
我有一个对象,它在函数内的许多不同函数中传递。这些函数可能会也可能不会改变对象的值,但是如果它们确实改变了它,那么我想获得对象的最新更改。
following is what im trying to do:
以下是我想要做的:
var ob = {text: 'this is me', name: 'john'}
function (object) {
changeObject(object);
customObjectChanger(object);
callback = function (object) {
object.text = 'new text';
}
callback(object);
// object value here should be object{text: 'new text', name: 'john'};
}
回答by Adam Rackis
In JavaScript objects are always passed by copy-reference. I'm not sure if that's the exactly correct terminology, but a copy of the reference to the object will be passed in.
在 JavaScript 中,对象总是通过复制引用传递。我不确定这是否是完全正确的术语,但是将传入对象引用的副本。
This means that any changes made to the object will be visible to you after the function is done executing.
这意味着在函数执行完成后,对对象所做的任何更改都将可见。
Code:
代码:
var obj = {
a: "hello"
};
function modify(o) {
o.a += " world";
}
modify(obj);
console.log(obj.a); //prints "hello world"
Having said that, since it's only a copy ofthe reference that's passed in, if you re-assignthe object inside of your function, this will notbe visible outside of the function since it was only a copy ofthe reference you changed.
话虽如此,由于它只是传入的引用的副本,如果您在函数内部重新分配对象,则这在函数外部将不可见,因为它只是您更改的引用的副本。
Code:
代码:
var obj = {
a: "hello"
};
function modify(o) {
o = {
a: "hello world"
};
}
modify(obj);
console.log(obj.a); //prints just "hello"
回答by newacct
"Objects" are not values in JavaScript, and cannot be "passed".
“对象”不是 JavaScript 中的值,不能“传递”。
All the values that you are dealing with are references (pointers to objects).
您正在处理的所有值都是引用(指向对象的指针)。
Passing or assigning a reference gives another reference that points to the same object. Of course you can modify the same object through that other reference.
传递或分配一个引用会给出另一个指向同一对象的引用。当然,您可以通过其他引用修改同一个对象。
回答by Ashish Singh Rawat
This is more explanation for Pass by value and Pass by reference (Javascript). In this concept, they are talking about passing the variable by reference and passing the variable by reference.
这是对按值传递和按引用传递(Javascript)的更多解释。在这个概念中,他们谈论的是通过引用传递变量和通过引用传递变量。
Pass by value (Primitive Type)
按值传递(原始类型)
var a = 3;
var b = a;
console.log(a); // a = 3
console.log(b); // b = 3
a=4;
console.log(a); // a = 4
console.log(b); // b = 3
- applies to all primitive type in JS(string, number, boolean, undefined, null).
a
is allocated a memory (say 0x001) andb
creates a copyof the value in memory (say 0x002).- So changing the value of a variable doesn't affect the other, as they both resides in two different locations.
- 适用于 JS 中的所有原始类型(字符串、数字、布尔值、未定义、空值)。
a
分配了一个内存(比如 0x001)并在内存中b
创建一个值的副本(比如 0x002)。- 所以改变一个变量的值不会影响另一个,因为它们都位于两个不同的位置。
Pass by reference (objects)
通过引用传递(对象)
var c = { "name" : "john" };
var d = c;
console.log(c); // { "name" : "john" }
console.log(d); // { "name" : "john" }
c.name = "doe";
console.log(c); // { "name" : "doe" }
console.log(d); // { "name" : "doe" }
- JS engine assigns the object to the variable
c
, it points to some memory say (0x012) - when
d=c
, in this step d points to the same location (0x012). - changing the value of any changes value for both the variable.
- functions are objects
- JS 引擎将对象分配给变量
c
,它指向一些内存,比如 (0x012) - 当
d=c
, 在这一步中 d 指向相同的位置 (0x012)。 - 改变两个变量的任何改变值的值。
- 函数是对象
Special case, Pass by reference (objects)
特殊情况,通过引用传递(对象)
c = {"name" : "jane"};
console.log(c); // { "name" : "jane" }
console.log(d); // { "name" : "doe" }
- The equal(
=
) operator sets up new memory space or address
- equal(
=
) 运算符设置新的内存空间或地址