Anj*_*nji 3 apache-flex flex3 actionscript-3
在flex中,如何检查变量是否存在?我试过用
if (this['some_variable'] != undefined) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
有一个运行时错误,说该属性some_variable不存在.我已经检查过,null而不是undefined,仍然是相同的错误.
请帮忙.
[编辑]
基于我使用过的回复this.hasOwnProperty('variable_name').我发现它返回trueif variable_name是一个public但是false如果它private/protected.如何检查私有变量?
有两种方法:
if ("some_variable" in this) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
它使用in运算符.
和:
if (this.hasOwnProperty("some_variable")) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
请参阅相关文档hasOwnProperty().
如果获取有关私有/受保护属性的信息,那么您无法通过Flash Player的当前状态获取此信息.我想,唯一可能的方法是某种运行时字节码操作.但据我所知,还没有人实现它.
但我有一个关于获取有关私有/受保护属性的信息的问题:您需要它的目的是什么?这些属性/方法的本质是你无法调用它们.即使你知道他们的存在.
您可以使用
if (this. hasOwnProperty("some_variable")) {
//access the variable inside
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4991 次 |
| 最近记录: |