Home > Uncategorized > if (value is Object) problem

if (value is Object) problem

January 8th, 2009 Leave a comment Go to comments

To my knowledge, everything is AS3 an object. Functions, Strings, Arrays…

So how do we check if our value is actually an Object? I had this problem in a for loop. I wanted to treat primitive types, Arrays and Objects differently. My first reaction was to test for Arrays, then for Objects and handle the rest in the else block. Well remember that if you check if a String is an Object, you’ll get a false positive.

Solution

var className:Object = getQualifiedClassName(value);
if (className == 'Object') {
    // do stuff here
}
else if (className == 'Array') {
    // do stuff here
}
else {
    // do stuff here
}
Tags: ,
  1. Josh Strike
    January 8th, 2009 at 21:46 | #1

    That’s an interesting approach.
    I wrote some code to do something similar to strict type amfphp server responses, without knowing what type of data we were going to be getting back at all… this is the guessing order that’s worked best for me…

    if (!re) {
    //it’s false or 0
    } else if (re === true) {
    //true bool
    } else if (re is Number) {
    //number
    } else if (re is String) {
    //you’re right, if you don’t check for strings now you get a false positive on object checks.
    } else if (re is Array) {
    //array
    } else {
    //treat as Object.
    }

  2. Anna
    January 9th, 2009 at 01:09 | #2

    In some cases, you might not want the 0 to be treated as a false value. Other than that, I think that it’s a fair approach, especially with the special case for Number.

    Only in my case I really needed to distinguish Objects and Arrays from all the rest.

    For AMF, your approach makes more sense. But just in case, I wouldn’t assume “else” to be an Object.

  3. Radu
    January 15th, 2009 at 08:36 | #3

    Hello Anna,

    Well there is as top level class function that does that. “typeof”

    var a:String = “annablog”;
    var b:Object = new Object()
    trace(typeof a) //string
    trace(typeof b) //object

    hope it helps! :)

    objects and arrays are both returning “object” but as both could be parse in the same way….i don’t think that is a problem for you…or is it?:D

  4. Radu
    January 15th, 2009 at 08:46 | #4

    Damn…sorry…didn’t read the post carefully:D…well…in that case:

    var a:Array = new Array();
    var b:Object = new Object();
    trace(a.hasOwnProperty(“length”))//true;
    trace(b.hasOwnProperty(“length”))//false;

    so…you simply test if your object has a specific property….i think you could do it in another way too…but this is the fastest one that i could think of:)

  5. Anna
    January 15th, 2009 at 08:56 | #5

    Sometimes I have a length property in my Objects. You usually can’t go wrong with getQualifiedClassName(), because unlike “is” it doesn’t go checking for all the parent classes.

  6. Radu
    January 15th, 2009 at 09:10 | #6

    you are right…still you can choose another property like “pop” or “push”…but yes…is kind of a hack thing….getQualifiedClassName() is the best solution I guess

  7. Radu
    January 15th, 2009 at 12:47 | #7

    Hei,

    I knew it must be a better way to do this since getQualifiedClassName() is mostly used to get custom classes names (like classes from assets in a library).And after a little research on livedocs…well it’s pretty simple in fact

    var array:Array = new Array();
    var obj:Object = new Object();
    trace(obj.constructor==Array);//false
    trace(obj.constructor==Object);//true

  8. Anna
    January 15th, 2009 at 13:25 | #8

    Checked it out and it does sound like a good solution. In Flex 2 it wasn’t officially documented but now it is in Flex 3: http://livedocs.adobe.com/flex/3/langref/Object.html#constructor

  9. TomX
    July 18th, 2011 at 16:44 | #9

    function var_clone(obj)
    {
    if (! ( typeof obj == ‘object’ ) || obj is Array ) return obj;

    var obj2 = { };

    for ( var k in obj ) obj2[k] = var_clone(obj[k]);

    return obj2;
    }

  1. No trackbacks yet.