对象是否在 javascript 深拷贝或浅拷贝中推入数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8660901/
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
Do objects pushed into an array in javascript deep or shallow copy?
提问by Travis J
Pretty self evident question...When using .push() on an array in javascript, is the object pushed into the array a pointer (shallow) or the actual object (deep) regardlessof type.
非常不言自明的问题...当在 javascript 中的数组上使用 .push() 时,无论类型如何,将对象推入数组是指针(浅)还是实际对象(深)。
回答by jfriend00
It depends upon what you're pushing. Objects and arrays are pushed as a pointer to the original object . Built-in primitive types like numbers or booleans are pushed as a copy. So, since objects are not copied in any way, there's no deep or shallow copy for them.
这取决于你在推动什么。对象和数组作为指向原始对象的指针推送。数字或布尔值等内置原始类型作为副本推送。因此,由于对象不会以任何方式复制,因此它们没有深拷贝或浅拷贝。
Here's a working snippet that shows it:
这是一个显示它的工作片段:
var array = [];
var x = 4;
let y = {name: "test", type: "data", data: "2-27-2009"};
// primitive value pushes a copy of the value 4
array.push(x); // push value of 4
x = 5; // change x to 5
console.log(array[0]); // array still contains 4 because it's a copy
// object reference pushes a reference
array.push(y); // put object y reference into the array
y.name = "foo"; // change y.name property
console.log(array[1].name); // logs changed value "foo" because it's a reference
// object reference pushes a reference but object can still be referred to even though original variable is no longer within scope
if (true) {
let z = {name: "test", type: "data", data: "2-28-2019"};
array.push(z);
}
console.log(array[2].name); // log shows value "test" since the pointer reference via the array is still within scope
回答by ruffin
jfriend00 is right on the mark here, but one small clarification: That doesn't mean you can't change what your variable is pointing to. That is, y
initially references some variable that you put into the array, but you can then take the variable named y
, disconnect it from the object that's in the array now, and connect y
(ie, make it reference) something different entirely without changing the object that now is referenced only by the array.
jfriend00 在这里恰到好处,但有一个小小的澄清:这并不意味着您不能更改您的变量所指向的内容。也就是说,y
最初引用了您放入数组中的某个变量,但是您随后可以获取名为 的变量y
,将其与现在数组中的对象断开连接,并连接y
(即,使其引用)完全不同的东西,而无需更改对象现在仅由数组引用。
http://jsfiddle.net/rufwork/5cNQr/6/
http://jsfiddle.net/rufwork/5cNQr/6/
var array = [];
var x = 4;
var y = {name: "test", type: "data", data: "2-27-2009"};
// 1.) pushes a copy
array.push(x);
x = 5;
document.write(array[0] + "<br>"); // alerts 4 because it's a copy
// 2.) pushes a reference
array.push(y);
y.name = "foo";
// 3.) Disconnects y and points it at a new object
y = {};
y.name = 'bar';
document.write(array[1].name + ' :: ' + y.name + "<br>");
// alerts "foo :: bar" because y was a reference, but then
// the reference was moved to a new object while the
// reference in the array stayed the same (referencing the
// original object)
// 4.) Uses y's original reference, stored in the array,
// to access the old object.
array[1].name = 'foobar';
document.write(array[1].name + "<br>");
// alerts "foobar" because you used the array to point to
// the object that was initially in y.