ActionScript - 确定值是否为类常量
如果某个特定函数在没有传递值的情况下无法工作,并且该值恰好是包含该函数的类的公共常量,我想抛出参数错误。
无论如何,有没有办法确定一个类是否拥有一个公共常量,而不必遍历所有常量?
像这样的东西:
public static const HALIFAX:String = "halifax";
public static const MONTREAL:String = "montreal";
public static const TORONTO:String = "toronto";
private var cityProperty:String;
public function set city(value:String):void
{
if (!this.hasConstant(value))
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
public function get city():Strig
{
return cityProperty;
}
目前,对于此功能,我必须像这样编写城市设置器函数:
public function set city(value:String):void
{
if (value != HALIFAX && value != MONTREAL && value != TORONTO)
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
这是完成此任务的唯一方法吗?
i'd like to throw an argument error if a particular function doesn't work without a passed value that also happens to be a public constant of the class containing the function.
is there anyway to determine if a class owns a public constant instead of having to iterate thru all of them?
something like this:
public static const HALIFAX:String = "halifax";
public static const MONTREAL:String = "montreal";
public static const TORONTO:String = "toronto";
private var cityProperty:String;
public function set city(value:String):void
{
if (!this.hasConstant(value))
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
public function get city():Strig
{
return cityProperty;
}
currently, for this functionality i have to write the city setter function like this:
public function set city(value:String):void
{
if (value != HALIFAX && value != MONTREAL && value != TORONTO)
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
is this the only way to accomplish this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,如果您使用反射:
请注意,要使其正常工作,所有常量必须始终是声明为
public static const
的 String 对象。Yes, if you use reflections:
Note that for this to work, all constants must always be String objects declared
public static const
.我自己正在寻找这个问题的答案,并发现
hasOwnProperty()
不适用于静态属性,这很烦人。但事实证明,如果将类强制转换为 Class 对象,它确实可以工作。这是一个例子:
我将此解决方案归功于此讨论中的 jimmy5804,所以向他致敬。
I was looking for an answer to this question myself and found it annoying that
hasOwnProperty()
did not work for static properties. Turns out though, that if you cast your class to aClass
object, it does work.Here's an example:
I owe this solution to jimmy5804 from this discussion, so hats off to him.
您应该能够使用括号表示法来执行此操作。例如:
或者更具体地针对您的情况:
您在这里唯一需要注意的是,如果您请求不存在的对象属性,括号访问器将抛出 ReferenceError ,例如:
但它不会如果您要求静态属性,则抛出:
所以在您的情况下您可以尝试:
编辑:
抱歉,我将 const 的名称与其值混淆了。
为了解决您的问题,您必须使 String 值与 const 的名称相同,例如:
然后您可以使用如上所述的查询,它会给您所需的结果。
You should be able to use bracket notation to do this. For example:
or more specific to your case:
The only thing you have to look out for here, is that the bracket accessor will throw a ReferenceError if you ask for an object property that isn't there, such as:
But it will not throw if you are asking for a static property:
So in your case you might try:
EDIT:
Sorry, I was confusing the const's names with their values.
For this to work on your problem you would have to make the String value the same as the const's name, so for example:
then you could use the query as described above and it would give you the desired result.