Skip to content

The “in” operator

2010 May 24
by Sven Busse

Juten Tach,

i work with Actionscript for some time now, but i wasn’t aware of that little thing. You can use “in” for checking, if an attribute/field exists in an object, like so:

package {
   import flash.display.Sprite;
 
   public class Test extends Sprite {
 
      public function Test() {
         var myObject:Object = {name: "foo", nothing: "bar"};
         trace("name" in myObject);     // true
         trace("anything" in myObject); // false
      }
   }
}

It also works for checking for fields in class instances. Might come in handy sometime :)

2 Responses leave one →
  1. May 24, 2010

    This is very helpful indeed, thx!

  2. May 24, 2010

    And this operator works much faster than Object.hasOwnProperty() method, which is created for the same purpose.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

*