关于AS3的for in遍历Object对象的属性问题

var funcAry:Array=new Array();

funcAry.push(aFunc);
funcAry.push(bFunc);
funcAry.push(cFunc);

function aFunc(eO:Object){
        eO.aFuncExected=true;
        trace("aFunc()");
}
function bFunc(eO:Object){
        eO.bFuncExected=true;
        trace("bFunc()");
}
function cFunc(eO:Object){
        eO.cFuncExected=true;
        trace("cFunc()");
}

var originObject:Object=new Object();
var operationAry:Array=[2,1,0];

for(var k:int=0;k<operationAry.length;k++){
        funcAry[operationAry[k]](originObject);
}

for(var j in originObject){
    trace(j+":"+originObject[j])
}

/*为什么输出结果是
cFunc()
bFunc()
aFunc()
aFuncExected:true
cFuncExected:true
bFuncExected:true*/
//originObject中加入属性的顺序是cFuncExected,bFuncExected,aFuncExected,为什么用for in遍历时就输出成了aFuncExected,cFuncExected,bFuncExected
/*而不是:
cFunc()
bFunc()
aFunc()
cFuncExected:true
bFuncExected:true
aFuncExected:true*/


得到答案:

Object用数组访问符实际使用了哈希表。
哈希表会将每个索引(字符串)重新编码,这个编码对程序员是不可知的
最后输出的顺序按重新编码为准。

故for..in只能保证哈希表中的每个单位都被访问到。
而不能保证是按添加顺序被访问到

posted @ 2011-07-21 07:50  梦羽纱  阅读(3326)  评论(1编辑  收藏  举报