Javascript for loop, for and for…in

有兴趣的朋友可以尝试一下下面的执行结果:

for…in loop:

var characterArr = new Array();

characterArr[0] = "a";
characterArr[5] = "b";
characterArr[10] = "c";

for (index in characterArr) {
    characterArr[parseInt(index) + 2] = "new character";
    alert("index=" + index + "  value=" + characterArr[index]);
    delete characterArr[5];
}
alert("separate here!");
for (index in characterArr) {
    alert("index=" + index + "  value=" + characterArr[index]);
}

以下代码尝试前要做好心理准备,我不是恶作剧…
for loop:

var characterArr = new Array();

characterArr[0] = "a";
characterArr[5] = "b";
characterArr[10] = "c";

for (var index = 0; index <  characterArr.length; index++) {
    characterArr[index + 2] = "new character";
    alert("index=" + index + "  value=" + characterArr[index]);
}

我的结论:for…in loop循环的只是对象的一个副本.而for loop循环的是对象本身.