# Problem about "for"

**URL:** <https://forum.kirupa.com/t/problem-about-for/5125>\
**Category:** flash\
**Created:** [September 2, 2002, 1:31am UTC](https://forum.kirupa.com/t/problem-about-for/5125 "2002-09-02T01:31:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imagebear](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@imagebear](https://forum.kirupa.com/u/imagebear)\
**Post date:** [September 2, 2002, 1:31am UTC](https://forum.kirupa.com/t/problem-about-for/5125/1 "2002-09-02T01:31:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 2, 2002, 2:27am UTC](https://forum.kirupa.com/t/problem-about-for/5125/2 "2002-09-02T02:27:20Z")

</div>

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,

```php

//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.

```php

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 2, 2002, 2:35am UTC](https://forum.kirupa.com/t/problem-about-for/5125/3 "2002-09-02T02:35:38Z")

</div>

[COLOR=red]thks david[/COLOR]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 2, 2002, 11:02am UTC](https://forum.kirupa.com/t/problem-about-for/5125/4 "2002-09-02T11:02:46Z")

</div>

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

pom :asian:
