Terrible Flash for each problem
Can someone explain to me why Flash would ever do this with a for each
Code:
//Create a prototype function on an object
Object.prototype.hello = function() {
trace("Hello World");
}
//Create a simple object
var bob:Object = {Name:"Bob", Age:"50"}
//Iterate over the object
for each(var thing in bob) {
trace(thing + " is " + typeof thing);
}
Here is the output for the above piece of code:
Quote:
Bob is string
50 is number
function Function() {} is function
Look carefully at that. Look at the bob object: it only has TWO values, a string and a number. But the output amazingly shows that bob has THREE values. And the last value is...an empty fuction! How could this be?
Flash thinks that the PROTOTYPE method is a variable in Object! It took me forever to track this down. I was about to go insane. It doesn't just happen for object, oh no. If you do a foreach on ANY class (dictionary, XMLList, what have you) with a prototype method, Flash screws up and thinks the prototype method is actually just a variable in the object, even though it only outputs a blank function.
So this is a bug in Flash, right? A prototype method shouldn't show up when looping through the object. By that logic, ALL methods of the object should show up in a for each. Does anyone have any suggestions for a workaround, or a better way to loop through Objects? I'll be here, growing my hair back.