先看代码:varfoo,bar;foo=newArray(8);bar=foo.map(function(){return1;});//注释一console.dir(bar);//=>[undefined×8]foo[6]=1;console.dir(foo);//=>[undefined×6,1,undefined×1]bar=foo.map(function(){return2;});console.dir(bar);//=>[undefined×6,2,undefined×1]不知各位大神看出问题没有,可能是我“功夫”不到家,也许本来就没有问题。我描述下问题:为什么注释一处,打印的结果不是[1,1,1,1,1,1,1,1]?。
2 回答
30秒到达战场
TA贡献1828条经验 获得超6个赞
foo=newArray(8);foo.forEach(function(v,i){console.log(v,i);});//没有输出foo[3]=1;foo.forEach(function(v,i){console.log(v,i);});//仅有一行输出:13foo=[];foo.length=8;foo.map(function(){return1;});//[undefinedx8]大概是跟Array的实现有关系。在IE(我用的11)上会有不同的效果。
添加回答
举报
0/150
提交
取消
