我正在构建一个Flex应用程序,我遇到了检测变量是否已初始化的问题。请观察此简化代码示例:
<fx:Script>
<![CDATA[
private var pageObject:* = null; //Yes the "*" data type is needed ;)
//Fired by the application elseware
private function constructMenu(e:ResultEvent):void {
if (this.pageObject != null) {
//This block never runs...
// The pageObject will be null when the application
// first runs, so skip this first time around.
// However... this method will be called multiple
// times. Since FoodMenu() is a display object
// and is instantiated below, all subsequent calls
// to this method will require us to remove the old
// display object before adding a new one.
} else {
// This block always runs...
}
setTimeout(function():void {
this.pageObject = new FoodMenu(); //Should now be not-null!!!
//Do more stuff with the FoodMenu() and add to the main application
}, 1000);
}
]]>
</fx:Script>
根据给定的代码示例和注释,有人可以解释为什么this.pageObject
始终注册为非null,无论调用constructMenu()
方法的次数是多少次{{1} class是实例化的吗?
感谢您的时间。
编辑:请求的完整MXML
FoodMenu()
答案 0 :(得分:2)
我看到的一个可能的问题是使用这段代码:
setTimeout(function():void {
this.pageObject = new FoodMenu(); //Should now be not-null!!!
//Do more stuff with the FoodMenu() and add to the main application
}, 1000);
“this”指针指向闭包中的其他东西(可能是全局对象)。
请尝试以下方法为自己寻找:
trace(this);
setTimeout(function():void {
trace(this);
},1000);