<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>你测试下就知道哪个最快了。。
{
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)
这个函数有什么特殊的用意?
to mingxuan3000(铭轩)
的那个。。修改的时候不影响copy的数组啊。。
@.@
迷糊ing...
{
return this.length ? [closure(this[0])].concat(this.slice(1).each(closure)) : [];
}
这样写的each效率高还是for循环的each效率高?
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>你测试下就知道哪个最快了。。
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>
不过对付对象是不行滴, 这个我目前没需要, 就不整了...