开发者

Actionscript object number of properties

开发者 https://www.devze.com 2023-02-04 16:09 出处:网络
How can I get the number 开发者_JS百科of properties in a generic Actionscript Object? (Like Array length)You will have to loop over all element to count them:

How can I get the number 开发者_JS百科of properties in a generic Actionscript Object? (Like Array length)


You will have to loop over all element to count them:

function objectLength(myObject:Object):int {
 var cnt:int=0;

 for (var s:String in myObject) cnt++;

 return cnt;
}

var o:Object={foo:"hello", bar:"world"};
trace(objectLength(o)); // output 2


Even shorter code here:

var o:Object={foo:"hello",bar:"world",cnt:2};
trace(o.cnt);  // output  2;

Just remember to update the very last argument in the object list if ever anything is added to it. That's the main downside to this approach, I think. And now, of course, the .cnt does not actually return the true list length, but rather it is the list length - 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消