复制对象 [英] Duplicating objects

查看:64
本文介绍了复制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在复制或提供

复制对象的一般机制?举个例子,假设我需要复制

一个数组。我可以用array.slice(0)完成这个,但是那不是

一般会削减。我还可以在Array

原型中添加一个复制方法,如下所示:


Array.prototype.copy = function(){

var newArray = [];

for(var idx = 0; idx< this.length; idx ++){

newArray.push(this [idx] );

}

返回newArray;

}


我可以写类似的副本( )各种对象的方法。但是我确信我不是第一个需要这种功能的程序员。这样做的规范方法是什么?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

Is there a general mechanism to duplicate, or provide for the
duplication of, objects? As an example, suppose I need to duplicate
an array. I can accomplish this with array.slice( 0 ), but that''s not
going to cut in general. I could also add a copy method to the Array
prototype, like so:

Array.prototype.copy=function() {
var newArray=[];
for( var idx=0; idx < this.length; idx++ ) {
newArray.push( this[idx] );
}
return newArray;
}

and I could write similar copy() methods for various objects. But I
feel certain that I''m not the first programmer who has had a need for
such functionality. What is the canonical way of doing this?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

Christopher Benson-Manica于2005年12月6日在comp.lang.javascript中写道
Christopher Benson-Manica wrote on 06 dec 2005 in comp.lang.javascript:
是否存在复制或提供对象重复的一般机制?举个例子,假设我需要复制一个数组。我可以用array.slice(0)来完成这个,但是那不是一般的削减。我还可以在Array
原型中添加一个复制方法,如下所示:

Array.prototype.copy = function(){
var newArray = [];
for(var idx = 0; idx< this.length; idx ++){
newArray.push(this [idx]);
}
返回newArray;
}
Is there a general mechanism to duplicate, or provide for the
duplication of, objects? As an example, suppose I need to duplicate
an array. I can accomplish this with array.slice( 0 ), but that''s not
going to cut in general. I could also add a copy method to the Array
prototype, like so:

Array.prototype.copy=function() {
var newArray=[];
for( var idx=0; idx < this.length; idx++ ) {
newArray.push( this[idx] );
}
return newArray;
}




Array.prototype.copy = function(){

返回this.concat();

}


或者只是我们一个空的.concat()本身:

var b = a.concat()

-

Evertjan。

荷兰。

(用我的电子邮件地址替换所有带点的十字架)



Array.prototype.copy=function() {
return this.concat();
}

or simply us an empty .concat() by itself:
var b = a.concat()
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)




Christopher Benson-Manica写道:

Christopher Benson-Manica wrote:
是否存在复制或提供对象重复的一般机制?举个例子,假设我需要复制一个数组。我可以用array.slice(0)来完成这个,但是那不是一般的削减。我还可以在Array
原型中添加一个复制方法,如下所示:

Array.prototype.copy = function(){
var newArray = [];
for(var idx = 0; idx< this.length; idx ++){
newArray.push(this [idx]);
}
返回newArray;
}

我可以为各种对象编写类似的copy()方法。但我确信我不是第一个需要这种功能的程序员。这样做的规范方法是什么?
Is there a general mechanism to duplicate, or provide for the
duplication of, objects? As an example, suppose I need to duplicate
an array. I can accomplish this with array.slice( 0 ), but that''s not
going to cut in general. I could also add a copy method to the Array
prototype, like so:

Array.prototype.copy=function() {
var newArray=[];
for( var idx=0; idx < this.length; idx++ ) {
newArray.push( this[idx] );
}
return newArray;
}

and I could write similar copy() methods for various objects. But I
feel certain that I''m not the first programmer who has had a need for
such functionality. What is the canonical way of doing this?




没有这样的建议可以随意;-)

JavaScript对象没有没有clone()方法。


所以在数组的情况下你的选项是:

1)如果数组中没有其他数组(psi-single-dimention)

你使用:

var arrayOneCopy = arrayOne.slice(0);


2)如果数组中确实有其他数组(psi-multi-dimention)你

使用你的代码但只需要*递归*并检查每一个单元

元素如果它不是数组。


3)如果你不介意JSON那么你可以


var arrayOneCopy = JSON.parse(JSON.stringify(arrayOne));



There is not such so feel free to propose one ;-)
JavaScript object doesn''t have clone() method.

So in case of array your options are:
1) If array doesn''t have other arrays in it (psi-"single-dimention")
you use:
var arrayOneCopy = arrayOne.slice(0);

2) If array does have other arrays in it (psi-"multi-dimention") you
use your code but just make it *recursive* and check for each single
element if it''s not an array.

3) Also if you don''t mind of JSON then you can

var arrayOneCopy = JSON.parse(JSON.stringify(arrayOne));


< 11 *********** ***********@g14g2000cwa.googlegroups .com>

VK< sc ********** @ yahoo.com>写道:
<11**********************@g14g2000cwa.googlegroups .com>
VK <sc**********@yahoo.com> wrote:
没有这样的建议可以随意提出;-)


肯定有人已经这样做了吗?我无法想象我是第一个想要重复对象的人b $ b ...

JavaScript对象没有clone()方法。


任何特殊原因?

2)如果数组中有其他数组(psi-multi-dimention)你
使用你的代码,但只是让它*递归*并检查每个单元
元素,如果它不是一个数组。
There is not such so feel free to propose one ;-)
Surely someone has already done so? I can''t imagine I''m the first
person who''s wanted duplicated objects...
JavaScript object doesn''t have clone() method.
Any particular reason for that?
2) If array does have other arrays in it (psi-"multi-dimention") you
use your code but just make it *recursive* and check for each single
element if it''s not an array.




好​​吧,只要因为我在提议......


Object.prototype.copy = function(deep){

返回此; //默认使用引用

}


Array.prototype.copy = function(deep){

var c = [ ];

for(var idx = 0; idx< this.length; idx ++){

if(deep){

c。 push(this [idx] .copy(true));

}

else {

c.push(this [idx]);

}

}

返回c;

}


-

Christopher Benson-Manica

ataru(at)cyberspace.org



Well, as long as I''m proposing things...

Object.prototype.copy=function( deep ) {
return this; // Use references by default
}

Array.prototype.copy=function( deep ) {
var c=[];
for( var idx=0; idx < this.length; idx++ ) {
if( deep ) {
c.push( this[idx].copy(true) );
}
else {
c.push( this[idx] );
}
}
return c;
}

--
Christopher Benson-Manica
ataru(at)cyberspace.org


这篇关于复制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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