Actionscript3:变量是否存在?

问题描述:

我对Actionscript有点新,但我无法弄清楚这一点。我在这个话题上做了大量的搜索,并没有找到明确的答案。我试过以下解决方案,人们在网上发布,但都没有工作。Actionscript3:变量是否存在?

以下所有解决方案给出错误:1120:未定义的属性MYVARIABLE的访问

建议#1:

try { 
    trace(myVariable); } 
catch { 
    trace("your variable doesn't exist"); } 

建议#2:

if (myVariable) { 
    trace("your variable exists!!"); } 
else { 
    trace("it doesn't exist"); } 

建议#3:

if (myVariable == null) 
    trace("your variable doesn't exist"); 

建议#4:

if (myVariable == undefined) 
    trace("your variable doesn't exist"); 

就像我说的,我发现很多论坛的帖子和作品放在网上,让上述建议说,他们将工作,但他们似乎都给了我相同的1120:访问未定义的属性myVariable错误。顺便说一句,如果你想知道为什么我需要检查一个变量是否存在,我打算在其URL中传递变量给SWF,所以我需要确保存在正确的变量并妥善处理代码,如果他们没有通过。


感谢您的快速回复。仍然没有真正的工作。变量的范围仅在脚本的顶部/根级别。基本上,我开始一个新的Flash文件,在第一帧我添加了以下行动:

// to check for this.myVariable 
if (this.hasOwnProperty("myVariable")) { 
    trace("myVariable exists"); 
} 
else 
{ 
    //Variable doesn't exist, so declare it now 
    trace("declaring variable now..."); 
    var myVariable = "Default Value"; 
} 

trace(myVariable); 

当我运行的Flash文件,我得到这样的输出:

myVariable exists 
undefined 

我期待这样的:

declaring variable now... 
Default Value 

By the way, in case you are wondering why I would need to check if a variable exists or not, I'm planning on passing variables to the SWF in its URL, so I need to make sure the proper variables exist and handle the code properly if they are not passed in.

然后你采取了错误的方法。以下是读取和验证SWF参数的正确方法,以及缺省值(如果它们不存在):

private function parameter(name:String, defaultValue:String):String 
{ 
     // Get parameter list 
    var paramObj:Object = LoaderInfo(stage.loaderInfo).parameters; 

     // Check if parameter exists 
    if(paramObj.hasOwnProperty(name) && paramObj[name] != "") 
     return paramObj[name];      
    else 
     return defaultValue; 
} 

警告!由于这是在'stage'属性上继承的,请使用此代码或者从文档类或Event.ADDED_TO_STAGE之后。

+0

这工作完美!谢谢! – 2009-09-27 05:59:21

+0

ONe为'正确的方式'注册商标。 – 2013-11-23 01:17:01

LiraNuna的答案肯定是访问装载机参数正确方式。

// to check for this.myVariable 
if (this.hasOwnProperty("myVariable")) { 
    trace("myVariable exists"); 
} else { 
    //Variable doesn't exist, so declare it now 
    trace("declaring variable now..."); 
    this.myVariable = "Default Value"; 
} 

trace(this.myVariable); 

这应包括您的情况:然而,要回答如何检查的变量(后人)的存在的问题,这与hasOwnProperty()方法,它存在于所有对象进行。但我不知道有任何方法通过直接对变量进行引用来检查变量的存在方式。我相信你必须通过它的范围来提及它。

fenomas可能是最正确的。 (我应该修改一些我的代码来使用该方法)。但目前我用于课堂的方法是:

try { 
    if(this["myFunction") { 
    this["myFunction"](); 
    } 
catch(e:Error) {} 

这不是最优雅的,因为它确实会抛出异常。

我使用这种类型的代码的原因是我们正在做一些动态编码并滥用flex中的“includes”来编写样板代码。

trace(myVariable);

当阶段不可用时,您可以尝试从root.loaderInfo.parameters提取参数。注意检查链条的每个属性是否为空。

if(root != null && root.loaderInfo != null && root.loaderInfo.parameters != null) { 
    myVar = root.loaderInfo.parameters.myVar; 
} 
+0

如果DisplayObject不是显示列表的一部分,'root'将不可用。 – LiraNuna 2011-02-12 01:00:59

if (myVariable === "undefined") { 
     trace("variable doesn't exist"); 
    else 
     trace("variable exists"); 

注意三重比较操作。这是我从Javascript时代学到的一个小技巧。同样适用于AS3,它对于检查数组元素是否存在即使为空也很有用。像这样:

var array:Array = ["zero", 1, null]; 
if (array[2]) //returns false 
if (array[2] !== "undefined") //returns true 

我最近遇到了同样的问题,并且我找到了解决这个问题的方法。然而,我发现,当你注册的变量是这样的...

var i:int; 

...它不会重置变量,如果已经做到了。例如,如果这是我的第1帧代码:

var i:int; 
if (i > 0){ 
    i++; 
} else { 
    i = 1; 
} 

那么这些将是“我”的每一次闪回到了相同的帧值:

// First Iteration 
trace(i); // 1 
// Second Iteration 
trace(i); // 2 
// Third 
trace(i); // 3, etc... 

它的工作在我自己的项目,这是一个很少的代码比以前的答案。我希望它也适用于你。