Problem about "for"

codes:<hr>function countProp(obj) {
var n = 0;
for (var p in obj) n++;
return n;
};<hr>

My problem is :
<hr> for (var p in obj) n++;<hr>

And who can explain that?

THKS!

Well… I still find that I run into errors when using the ‘for’ loops… (and it is a loop of sorts) which is why I don’t normaly talk about them too much. They are very cool though, and vital to flashing once you get to a level.

The premis is that any object in Flash has certain properties which the flash player can read. The for loop takes advantage of this.

for (var p in obj) n++;

says… for each property of the object named in the method, assign it’s value to the variable ‘p’.

when taken in context,


//create function called countProp with a variable which
//it takes called obj
function countProp(obj) {
//set a variable called n equal to 0
var n = 0;
//set a for loop to search this objects entire
//structure for properties. hint other movie
//clips are properties too
for (var p in obj) n++;
//return to whatever called this function the current
//value of n
return n;
};

the problem I see with it is that it doesn’t pull p out. I use arrays to keep track of the data. Something like this.


objsProperties = new Array();
function countProp(obj) {
var n = 0;
for (var p in obj) n++;
objsProperties[n]=p;
};

This way I have a complete list of all the objects properties in a numbered list. Of course I have no clue what this script fits into as a whole so there’s no saying for sure what the author is returning n for. Perhaps he’s just looking for the number of properties that the object has. He could then use a ‘for…in’ loop really easily, to search through the object a second time.

[COLOR=red]thks david[/COLOR]

Err… There’s something about it in the AS tricks too.

pom :asian: