tags:

views:

46

answers:

2

Is there any way to mimic the in operator, but testing for the existence of protected or private fields?

For example, this:

<mx:Script><![CDATA[
    public var pub:Boolean = true;
    protected var prot:Boolean = true;
    private var priv:Boolean = true;
]]></mx:Script>

<mx:creationComplete><![CDATA[
    for each (var prop in ["pub", "prot", "priv", "bad"])
        trace(prop + ":", prop in this);
]]></mx:creationComplete>

Will trace:

pub: true
prot: false
priv: false
bad: false

When I want to see:

pub: true
prot: true
priv: true
bad: false
+1  A: 

How about:

  <mx:creationComplete>
    for each (var prop:String in ["pub", "prot", "priv", "bad"])
    {
      try
      {
        t.text += prop + ":" + this[prop] + "\n";
      }
      catch (e:Error)
      {
        t.text += prop + ": false" + "\n";
      }
    }
  </mx:creationComplete>
James Ward
since you have the required connections, could you maybe kindly ask to change this behaviour? :) because this is really quite sad.
back2dos
Yea, this is what I'm doing now: `function hasProp(name) { try { this[name]; return true; } catch (e:ReferenceError) { /* do nothing */ }; return false; }`… But it's pretty lame :(
David Wolever
Please file a feature request: http://bugs.adobe.com
James Ward
+1  A: 

you can just try to access it and catch resulting errors. :)

in is unaware of any namespaces currently opened (including private and protected in your case), and will only look within the public namespace.

in for objects actually just calls Object::hasOwnProperty. Unfortunately, you effectively cannot override this method to alter its behaviour. the only class that can influence it is flash.utils::Proxy, which actually uses flash_proxy::hasProperty to to determine the return value of hasOwnproperty. So no, other than trying, there's no other way sadly.

greetz
back2dos

back2dos
Cool - that's good to know. Thanks.
David Wolever