简体   繁体   English

如何检查flex中是否存在变量

[英]How to check if a variable exists in flex

In flex, how to check if a variable exists? 在flex中,如何检查变量是否存在? I have tried using 我试过用

if (this['some_variable'] != undefined) {
    //do something
}

There is a run time error saying the property some_variable does not exists. 有一个运行时错误,说属性some_variable不存在。 I have checked with null instead of undefined , still the same error. 我已经检查过null而不是undefined ,仍然是同样的错误。

please help. 请帮忙。

[EDIT] [编辑]

Based on the replies I have used this.hasOwnProperty('variable_name') . 根据回复,我使用了this.hasOwnProperty('variable_name') I found that its returning true if variable_name is a public but false if its private/protected . 我发现如果variable_namepublic ,则返回true如果是private/protected则返回false How to check for a private variable? 如何检查私有变量?

There are two ways for that: 有两种方法:

if ("some_variable" in this) {
    //do something
}

It uses in operator . 它使用in运营商

And: 和:

if (this.hasOwnProperty("some_variable")) {
    //do something
}

See documentation about hasOwnProperty() . 请参阅有关hasOwnProperty()文档

What about getting information about private/protected properties the situation is that you can't get this info with the current state of Flash Player. 如果获取有关私有/受保护属性的信息,那么您无法通过Flash Player的当前状态获取此信息。 The only possible way, I suppose, is some kind of runtime bytecode manipulation. 我想,唯一可能的方法是某种运行时字节码操作。 But as far as I know nobody implemented it yet. 但据我所知,还没有人实现它。

But I have a question about getting info about private/protected properties: for what purpose you need it? 但我有一个关于获取有关私有/受保护属性的信息的问题:您需要它的目的是什么? The nature of these properties/methods is you can't call them. 这些属性/方法的本质是你无法调用它们。 Even if you know about their existence. 即使你知道他们的存在。

You can use 您可以使用

if (this. hasOwnProperty("some_variable")) {
    //access the variable inside
}

if (this.hasOwnProperty('some_variable')) DO_IT_!()

Explanation: 说明:

this['some_variable'] tries to evaluate the value of the instance property some_variable . this['some_variable']试图评估实例属性some_variable的值。 If there is no such a property, you will get this error. 如果没有这样的属性,您将收到此错误。

To test if a property exists for a particular object use hasOwnProperty or wrap your condition in a try/catch block or use if ('some_variable' in this) . 要测试某个特定对象是否存在属性,请使用hasOwnProperty或将您的条件包装在try/catch块中,或者使用if ('some_variable' in this)

Usually you create an object property in a class file: 通常在类文件中创建一个对象属性:

public class MyClass {
   public var myProperty : String = "ich bin hier";
}

Then you refer to that property within the class: 然后你在类中引用该属性:

trace (myProperty);
trace (this.myProperty);

Using the array syntax [] is also possible but will throw the error if the property is not defined. 使用数组语法[]也是可能的,但如果未定义属性,则会抛出错误。

trace (this['myProperty']);

And finally! 最后! If you declare your class to be dynamic you might use the array syntax even if the property does not exist. 如果将类声明为动态,则即使该属性不存在,也可以使用数组语法。

public dynamic class MyClass {
   public function MyClass() {
       trace (this["never_declared_property"]);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM