Flash Weirdness of the Day II
Hit a brand new FlashWoD today! This took me a while to track down, mainly because I couldn’t actually believe that it could possibly be true. In Actionscript 3, an empty array created with object literal notation as shown below will evaluate as equal to an empty string. I had a part of a data structure that would be an empty string if uninitialized, and would be an array otherwise.
The code below will output “The world has gone mad!”, and rightly so, I think. The theory is that the empty array is converted into a string for comparison.
var obj:Object = {
"array": []
};
if(obj.array == '')
{
trace('The world has gone mad!');
}
else
{
trace('The world may not be altogether mad.');
}
Brian did an experiment that showed that a literal array below would display the same behavior. Note that it must be assigned to a variable of type Object
, otherwise the comparison won’t compile (which is something, I guess):
var obj:Object = [];
if(array == '')
{
trace('The world has gone mad!');
}
else
{
trace('The world may not be altogether mad.');
}