如何复制一个数组?原来的数组改动不影响复制的那个数组
在一个页面用showModalDialog打开对话框
returnArray = window.showModalDialog( "a.jsp ",window, "dialogHeight:700px;dialogWidth:910px;resizable:yes;status:no;help:no; ");
打开的页面,把原页面的一个数组赋值给一个临时变量
var returna = dialogArguments.arr_obj; //用于原样返回
如果点原样返回按钮,就返回returna;
但是如果我对arr_obj有修改,返回的returna也是修改过的,如何返回一个没修改过的returna?
------解决方案--------------------Array.prototype.each = function(closure)
{
return this.length ? [closure(this[0])].concat(this.slice(1).each(closure)) : [];
}
var a=new Array();
a[0]= "a "
a[1]= "b "
var c=a.each(function(x){return x})
alert(c)
a[0]= "b "
alert(a)
alert(c)
------解决方案-------------------- <script type= "text/javascript ">
Array.prototype.each = function(closure) {
return this.length ? [closure(this[0])].concat(this.slice(1).each(closure)) : [];
};
Array.each = function (r, a) {
for (var i = 0 ; i < a.length ; i ++)
r[r.length] = a;
}
var a = [];
for (var i = 0 ; i < 50500 ; i ++)
a[i] = i;
var t = new Date;
Array.each([], a);
alert(new Date - t + "ms ");
var t = new Date;
c = a.slice(0);
alert(new Date - t + "ms ");
a.splice(0, 50000);
var t = new Date;
var c = a.each(function(x){return x})
alert(new Date - t + "ms ");
</script>
你测试下就知道哪个最快了。。
------解决方案--------------------Array.prototype.each = function(closure) {
return this.length ? [closure(this[0])].concat(this.slice(1).each(closure)) : [];
};
//这种递归的方法效率低 而且内存占用很厉害 为什么用这种写法?
Array.prototype.Each = function(closure) {var r=new Array();
for(var i=0,l=a.length;i <l;i++)r[r.length]=closure(this[i]);
return r;
}
//同样的参数为什么不用for循环?
------解决方案--------------------看看, 最简单的 字串数组 复制方法...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<!-- DW6 -->
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=utf-8 " />
<title> shawl.qiu template </title>
<script type= "text/javascript ">
// <![CDATA[
var ar1 = [1,2,3];
var iCount = ar1.length;
var sDelimiter = "SDF@#$SDFASSFad ";
var sAr1 = ar1.join(sDelimiter)
var ar2 = sAr1.split(sDelimiter);
defaultStatus =
"ar1: "+ar1
+ " ar2: "+ar2
;
for(var i=0; i <10; i++)
{
var iIndex = ar2.length;
ar2[iIndex] = iIndex;
}
defaultStatus +=
" ar1: "+ar1
+ " ar2: "+ar2
;
//]]>
</script>
</head>
<body>
</body>
</html>
不过对付对象是不行滴, 这个我目前没需要, 就不整了...