问题描述
是否存在复制或提供
复制对象的一般机制?举个例子,假设我需要复制
一个数组。我可以用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.
推荐答案
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)
没有这样的建议可以随意;-)
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));
好吧,只要因为我在提议......
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
这篇关于复制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!