我想获得我的号码的声明类型,但我不知道如何获得它们。所以我用可能的检测方法写了一个测试:
logNumber(Number(3.5), "Number");
logNumber(Number(3), "Number");
logNumber(Number(-3), "Number");
logNumber(uint(3), "uint")
logNumber(int(3), "int")
logNumber(int(-3), "int")
function logNumber(value:*, expected:String):void
{
trace("\n\n\n ");
trace("** Input value: " + value + "\n** Expected: " + expected + "\n")
trace("getQualifiedClassName: ", getQualifiedClassName(value) + check(getQualifiedClassName(value), expected));
switch (value)
{
case value as uint:
{
trace('as: uint' + check('uint', expected));
break;
}
case value as int:
{
trace('as: int' + check('int', expected));
break;
}
case value as Number:
{
trace('as: Number' + check('Number', expected));
break;
}
}
if(value is uint) trace("is: uint" + check('uint', expected));
else if(value is int) trace("is: int" + check('int', expected));
else if(value is Number) trace("is: Number" + check('Number', expected));
trace("describeType name:" + describeType(value).@name + check(describeType(value).@name, expected));
trace("typeof: ", typeof(value) + check(typeof(value), expected));
trace("\n" + describeType(value))
}
function check(type:String, expectedType:String):String
{
return "\n » " + (type == expectedType ? "good" : (type.toLowerCase() == expectedType.toLowerCase() ? "almost good" : "wrong"))
}
这会将以下结果输出到我的跟踪面板:
3.5 作为数字
** Input value: 3.5
** Expected: Number
getQualifiedClassName: Number
» good
as: Number
» good
is: Number
» good
describeType name:Number
» good
typeof: number
» almost good
<type name="Number" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
3 作为数字
** Input value: 3
** Expected: Number
getQualifiedClassName: int
» wrong
as: uint
» wrong
is: uint
» wrong
describeType name:int
» wrong
typeof: number
» almost good
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
-3 作为数字
** Input value: -3
** Expected: Number
getQualifiedClassName: int
» wrong
as: int
» wrong
is: int
» wrong
describeType name:int
» wrong
typeof: number
» almost good
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
3 作为单位
** Input value: 3
** Expected: uint
getQualifiedClassName: int
» wrong
as: uint
» good
is: uint
» good
describeType name:int
» wrong
typeof: number
» wrong
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
3 作为整数
** Input value: 3
** Expected: int
getQualifiedClassName: int
» good
as: uint
» wrong
is: uint
» wrong
describeType name:int
» good
typeof: number
» wrong
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
-3 作为整数
** Input value: -3
** Expected: int
getQualifiedClassName: int
» good
as: int
» good
is: int
» good
describeType name:int
» good
typeof: number
» wrong
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
有很多要分析的,但有些事情正在弹出:
- 如果一个 Number 等于 3.5,它将作为 Number 输出,但如果它是 3,它将作为 int/uint 输出。
- 很难检测它是 uint 还是 int,结果令人困惑且不一致。
出于调试目的(并且只是为了了解其工作原理),我想要我声明的类型。如何创建一个返回正确类型的函数?