在JS中创建(对象)的真实副本(可能吗?) [英] Creating true copies (of objects) in JS (possible?)

查看:86
本文介绍了在JS中创建(对象)的真实副本(可能吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里乱用一些代码...让我说我有这个数组:


a1 = [1," 2",new Array(2.5,3) ,3.5),4];


我在上面应用slice(0)来创建副本:


a2 = a1。切片(0);


但这不是真正的副本。如果我去a1 [2] [1] = 42,然后

警报(a2 [2] [1])我也会看到42。我很怀疑,但我还是想知道这个问题是否有解决方案?


问候,

Svend

I''m messing with some code here... Lets say I have this array:

a1 = [1,"2",new Array(2.5,3,3.5),4];

And I apply slice(0) on it, to create a copy:

a2 = a1.slice(0);

But this isn''t a true copy. If I go a1[2][1] = 42, and then
alert(a2[2][1]) I will see 42 there too. I''m doubting, but I was
wondering if there is a general solution to this problem?

Regards,
Svend

推荐答案



" svend" < SV ******** @ svendtofte.com> schreef in bericht

新闻:ef ************************** @ posting.google.c om ...

"svend" <sv********@svendtofte.com> schreef in bericht
news:ef**************************@posting.google.c om...

但这不是真正的副本。如果我去a1 [2] [1] = 42,然后
警告(a2 [2] [1])我也会看到42。我很怀疑,但是我想知道这个问题是否有通用解决方案?

But this isn''t a true copy. If I go a1[2][1] = 42, and then
alert(a2[2][1]) I will see 42 there too. I''m doubting, but I was
wondering if there is a general solution to this problem?




问题是由阵列中的数组引起的。当slice返回非数组元素的

副本时,数组元素仍然是引用。


您可以尝试使用检查类型的方法元素和

对数组元素应用slice()来提供副本:


Array.prototype.clone = function(){

var tmp = [];

for(i in this){

if(this [i] .constructor == Array){

tmp [i] = this [i] .slice(0);

} else {

tmp [i] = this [i];

}

}

返回tmp;

}


var a1 = [ 1,2,新数组(2.5,3,3.5),4];

var a2 = a1.clone();

a1 [2] [ 1] = 42;

alert(a2 [2] [1]);


请注意,此示例不适用于数组元素中的数组。

JW



The problem is caused by the array within the array. While slice returns a
copy of the non-array elements, array elements remain references.

You could try to use a method that checks the types of the elements and
applies slice() upon array elements to provide a copy:

Array.prototype.clone = function () {
var tmp = [];
for (i in this) {
if (this[i].constructor == Array) {
tmp[i] = this[i].slice(0);
} else {
tmp[i] = this[i];
}
}
return tmp;
}

var a1 = [1,"2",new Array(2.5,3,3.5),4];
var a2 = a1.clone();
a1[2][1] = 42;
alert(a2[2][1]);

Note that this example doesn''t work with arrays within array elements.
JW


" Janwillem Borleffs" < jw@jwscripts.com>写道:
"Janwillem Borleffs" <jw@jwscripts.com> writes:
Array.prototype.clone = function(){
var tmp = [];
for(i in this){
if (this [i] .constructor == Array){
tmp [i] = this [i] .slice(0);


为什么不

tmp [i] =这[i] .clone();



请注意,此示例不适用于数组元素中的数组。
Array.prototype.clone = function () {
var tmp = [];
for (i in this) {
if (this[i].constructor == Array) {
tmp[i] = this[i].slice(0);
Why not
tmp[i] = this[i].clone();
?
Note that this example doesn''t work with arrays within array elements.




然后它会。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。 ''



Then it would.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


2003年11月2日,在太阳报上,Lasse Reichstein Nielsen写道:
On Sun, 2 Nov 2003, Lasse Reichstein Nielsen wrote:
数组.prototype.clone = function(){
var tmp = [];
for(i in this){
if(this [i] .constructor == Array){
tmp [i] = this [i] .slice(0);
Array.prototype.clone = function () {
var tmp = [];
for (i in this) {
if (this[i].constructor == Array) {
tmp[i] = this[i].slice(0);



为什么不
tmp [i] = this [i] .clone();



Why not
tmp[i] = this[i].clone();
?




循环到ne的方法sted数组元素仅适用于

基元。我正在寻找一种更通用的方法(没有希望

有一个)。据我所知,没有办法

cloning x = new MyOwnObject();


想象一下如果数组本身就是一个元素!


它与咖喱功能相结合我对此很感兴趣。如果

无法确保累积的参数完整性,可能需要特别小心

,因为某些代码可能会产生无意的副作用。

问候,

Svend



The method of looping into nested array elements will only work for
primitives. I was looking for a more generic method (without hope
of there being one). As far as I can tell, there''s no way of
cloning x = new MyOwnObject();

And imagine if an array had itself as an element!

It''s in conjunction with a curry function I''m interested in this. If there
is no way to assure the accumulated arguments integrity, special care
might be needed, as some code might have unintentional side effects.

Regards,
Svend


这篇关于在JS中创建(对象)的真实副本(可能吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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