开发者

ActionScript: Using 'in' on protected/private variables?

开发者 https://www.devze.com 2022-12-26 18:16 出处:网络
Is there any way to mimic the in operator, but testing for the existence of protected or private fields?

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

For example, this:

<mx:Script>&开发者_StackOverflow中文版lt;![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


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消