I have an array say
我有一个数组。
var list = ["first", "second"];
now I assign list to some other variable say
现在我把列表赋给另一个变量。
var temp = list;
Now when I use splice on list like
现在当我在列表中使用splice时
list.splice(0,1);
Now when I check the value of list it shows
现在,当我检查列表的值时,它会显示出来
list = ["second"]
Also when I check the value of temp then it says
当我检查temp的值时,它会说
temp = ["second"]
I want to know why is that so? Why the value of temp is changed?
我想知道为什么是这样?为什么要更改temp的值?
4 个解决方案
#1
4
when using var temp = list
you're creating a reference temp
to list
. So since splice
changes list
array in-place, you're also changing temp
当使用var temp = list时,您正在创建要list的引用temp。因此,既然splice改变了列表数组,那么您也在改变temp。
Use slice instead which returns a copy of the array, like so
使用slice返回数组的一个拷贝,就像这样
var temp = list.slice();
#2
1
A common mistake in JS
JS中常见的错误
This is a pointer, not a clone of the object:
这是一个指针,不是对象的克隆:
var temp = list;
If you want to actually copy the object, there are a few ways. The simplest is just to concat it with itself:
如果你想复制这个对象,有几种方法。最简单的办法就是把它和它自己联系起来:
var temp = list.concat([]);
// or
var temp = list.slice();
Note that this is somewhat dangerous, it only gets the base values out of the array. There are more advanced methods for cloning objects and creating 'perfect' array clones.
注意,这有点危险,它只从数组中获取基值。还有更高级的方法可以克隆对象并创建“完美”的数组克隆。
#3
1
片
should do the trick:
应该是可以奏效的:
var temp = list.slice(0);
var temp = list.slice(0);
About object cloning, look here How do you clone an Array of Objects in Javascript?
关于对象克隆,请看这里,如何在Javascript中克隆对象数组?
#4
0
You can use a prototype method to add this functionality.
您可以使用一个原型方法来添加这个功能。
Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};
Which then allows you to do this:
这样你就可以做到:
var a = [1,2,3];
var b = a.clone();
a[1] = 5;
console.log(a); //1,5,3
console.log(b); //1,2,3
Disclaimer: shamelessly borrowed from here (the whole article is worth a read): http://my.opera.com/GreyWyvern/blog/show.dml/1725165
免责声明:无耻地从这里借用(整篇文章值得一读):http://my.opera.com/GreyWyvern/blog/show.dml/1725165
#1
4
when using var temp = list
you're creating a reference temp
to list
. So since splice
changes list
array in-place, you're also changing temp
当使用var temp = list时,您正在创建要list的引用temp。因此,既然splice改变了列表数组,那么您也在改变temp。
Use slice instead which returns a copy of the array, like so
使用slice返回数组的一个拷贝,就像这样
var temp = list.slice();
#2
1
A common mistake in JS
JS中常见的错误
This is a pointer, not a clone of the object:
这是一个指针,不是对象的克隆:
var temp = list;
If you want to actually copy the object, there are a few ways. The simplest is just to concat it with itself:
如果你想复制这个对象,有几种方法。最简单的办法就是把它和它自己联系起来:
var temp = list.concat([]);
// or
var temp = list.slice();
Note that this is somewhat dangerous, it only gets the base values out of the array. There are more advanced methods for cloning objects and creating 'perfect' array clones.
注意,这有点危险,它只从数组中获取基值。还有更高级的方法可以克隆对象并创建“完美”的数组克隆。
#3
1
片
should do the trick:
应该是可以奏效的:
var temp = list.slice(0);
var temp = list.slice(0);
About object cloning, look here How do you clone an Array of Objects in Javascript?
关于对象克隆,请看这里,如何在Javascript中克隆对象数组?
#4
0
You can use a prototype method to add this functionality.
您可以使用一个原型方法来添加这个功能。
Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};
Which then allows you to do this:
这样你就可以做到:
var a = [1,2,3];
var b = a.clone();
a[1] = 5;
console.log(a); //1,5,3
console.log(b); //1,2,3
Disclaimer: shamelessly borrowed from here (the whole article is worth a read): http://my.opera.com/GreyWyvern/blog/show.dml/1725165
免责声明:无耻地从这里借用(整篇文章值得一读):http://my.opera.com/GreyWyvern/blog/show.dml/1725165