【问题标题】:Is there a way to tell if a Function returns a value?有没有办法判断一个函数是否返回一个值?
【发布时间】:2017-02-21 06:28:02
【问题描述】:

有没有办法判断函数是返回值还是返回 void?

我尝试过使用 describeType,这是它返回的内容:

<type name="builtin.as$0::MethodClosure" base="Function" isDynamic="false" isFinal="true" isStatic="false">
  <extendsClass type="Function"/>
  <extendsClass type="Object"/>
  <accessor name="length" access="readonly" type="int" declaredBy="Function"/>
  <accessor name="prototype" access="readwrite" type="*" declaredBy="builtin.as$0::MethodClosure"/>
</type>

我已经传入了一个返回值的函数和一个返回 void 的函数。两者都返回上面相同的 XML 值。

下面是函数和代码:

public function getElementCount(o:Object, count:String):String {

    return count;
}

public function getElementCount2(object:Object, count:String):void {

}

var o:Object = mx.utils.DescribeTypeCache.describeType(getElementCount).typeDescription;
var o2:Object = mx.utils.DescribeTypeCache.describeType(getElementCount2).typeDescription;

【问题讨论】:

  • 你能不能运行一点代码来请求返回值,然后根据它抛出的错误类型判断它是否期望一个值?

标签: actionscript-3 flash air


【解决方案1】:

只需将getQualifiedClassName(input) 与您的函数一起用作输入。

示例代码:

package  
{
    import flash.display.MovieClip;
    import flash.utils.getQualifiedClassName;

    public class Get_Return_Type extends MovieClip 
    {

        public function Get_Return_Type() 
        {
            //# Check 1
            trace("check : function getElementCount");
            trace( "returns type : " + getQualifiedClassName( getElementCount(null, "test") ) );

            //# Check 2
            trace("check : function  getElementCount2");
            trace( "returns type : " + getQualifiedClassName(  getElementCount2(null, "test") ) );

        }

        public function getElementCount(o:Object, count:String):String 
        { return count; }

        public function getElementCount2(object:Object, count:String):void 
        { }


    }
}

跟踪结果:

检查:函数 getElementCount

返回类型:String

检查:函数 getElementCount2

返回类型:void

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 2012-04-17
    • 2017-09-05
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    相关资源
    最近更新 更多