Actionscript 3 如何在actionscript中声明全局变量

Actionscript 3 如何在actionscript中声明全局变量,actionscript-3,apache-flex,actionscript,Actionscript 3,Apache Flex,Actionscript,我期待着一些讽刺,但见鬼。我搜索了actionscript参考页面,似乎找不到如何声明简单的全局变量 as3中没有全局变量。但是静态变量可以在不创建类实例的情况下访问,因此它们可以用作全局变量 package { class MyClass { // ... public static var myStaticVar: Number; // ... } } 然后,您可以在程序中的任何位置访问变量myStati

我期待着一些讽刺,但见鬼。我搜索了actionscript参考页面,似乎找不到如何声明简单的全局变量

as3中没有全局变量。但是静态变量可以在不创建类实例的情况下访问,因此它们可以用作全局变量

package 
{
     class MyClass
     {
         // ...
         public static var myStaticVar: Number;
         // ...
     }
}
然后,您可以在程序中的任何位置访问变量myStaticVar,如下所示:

MyClass.myStaticVar = 5;
// ...
var value:Number = MyClass.myStaticVar;
全球对什么

如果您想要声明一个类的“全局”变量;您可以只使用任何变量:

public var myClassGlobal : Object = new Object();
这个变量可以在类中的任何地方访问;因为我将其公开,所以它也可用于任何可以访问该类实例的类。您可以这样访问它:

trace(myClassInstance.myClassGlobal);
trace((FlexGlobals.topLevelApplication as myMainApplicationFile).myFlexApplicationGlobal);
trace(MyClassWithStaticVariable.myStaticGlobal);
如果要声明Flex应用程序的“全局”变量,可以在主应用程序文件中声明变量:

public var myFlexApplicationGlobal :Object = new Object():
您可以使用在代码中的任何位置访问此值。大概是这样的:

trace(myClassInstance.myClassGlobal);
trace((FlexGlobals.topLevelApplication as myMainApplicationFile).myFlexApplicationGlobal);
trace(MyClassWithStaticVariable.myStaticGlobal);
像这样的方法通常被认为是封装no;因为它使类依赖于主应用程序文件,并最大限度地减少重复使用。这通常是我们试图避免的

您可以创建一个静态变量,并使用该类在任何地方访问它。静态变量未绑定到类的实例:

public static var myStaticGlobal :Object = new Object():
您可以这样访问它:

trace(myClassInstance.myClassGlobal);
trace((FlexGlobals.topLevelApplication as myMainApplicationFile).myFlexApplicationGlobal);
trace(MyClassWithStaticVariable.myStaticGlobal);

您还可以使用全局变量创建一个单例类,并使用框架(如Swiz或RobotLegs)将该类注入到需要它的类中。一个快速的谷歌搜索应该会显示你在Flex中创建单例的信息;在更大的编程社区范围内,有很多支持和反对单身的讨论

已经有几个好的答案了。我只想指出,
您可以在AS3中创建全局变量。只需在classes文件夹的根目录中创建一个文件,例如MyGlobal.as:

package {
    public var MyGlobal:String = "bla";
}
您可以通过
MyGlobal
访问它,因为它位于最顶层的包中。这项技术可以用在几种不那么具有破坏性的方法上。例如,您可以有一个类似于单例的全局常量,但它不是静态的,而是某个类的实例:

package {
    public const MySingleton:IMySingleton = new MySingletonImpl();
}

更新;不是从原来的海报 我以前从未听说过这一点,因此我收集了一个快速样本:

在根目录中:

package
{
    public var MyGlobal:String = "bla";
}
测试类:

package com.flextras.stackOverflow
{
    public class MyGlobalTest
    {
        public function MyGlobalTest()
        {
            trace(MyGlobal);
        }
    }
}
以及测试应用程序:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" initialize="windowedapplication1_initializeHandler(event)">
    <fx:Script>
        <![CDATA[
            import com.flextras.stackOverflow.MyGlobalTest;

            import mx.events.FlexEvent;

            protected function windowedapplication1_initializeHandler(event:FlexEvent):void
            {
                trace(MyGlobal);
                var a :MyGlobalTest = new MyGlobalTest();
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:WindowedApplication>


运行应用程序,跟踪确实会以正确的“bla”值显示两次

在方法闭包中,关键字
返回对“全局”对象的引用。
package com.appcloud9.utils
{
    public class GlobalReference
    {
        public static function get global() : Object
        {
            var getGlobal : Function = function() : Object
            {
                return this;
            };
            return getGlobal();
        }
    }
}

/*-|-||-|-||-|-||-  usage examples  -||-|-||-|-||-
 * the examples below focus on giving global access to the main Stage instance
 * but anything added to the global object ( which always exists no matter what )
 * is accessible via the same mechanisms
*/

// global stage reference added in main document class upon Event.EXIT_FRAME :
GlobalReference.global.stage = stage;

// later in a closure
var signalLightsOut = new Signal();
signalLightsOut.add( function() : void
{
    /* because the [ object global ] truly is 'global' all closures have
     * direct access to any properties added to it ( it is a dynamic class )
    */ 
    trace( stage );                        // [object Stage]
} );

// later in a constructor - before the class has a stage of it's own
public function MyConstructor()
{
    trace( stage );                        // null
    trace( GlobalReference.global.stage ); // [object Stage]
}
下面是从方法闭包内部检查
时的一些有趣结果

trace( this );    // outputs : [object global]

trace( flash.utils.describeType( this ) );

/* outputs :
description:
<type name="global" base="Object" isDynamic="true" isFinal="true" isStatic="false">
  <extendsClass type="Object"/>
  <constant name="Boolean" type="Boolean"/>
  <constant name="Namespace" type="Namespace"/>
  <constant name="undefined" type="*"/>
  <constant name="Number" type="Number"/>
  <constant name="USE_ITRAITS" type="uint" uri="avmplus"/>
  <constant name="Vector" type="__AS3__.vec::Vector" uri="__AS3__.vec"/>
  <constant name="uint" type="uint"/>
  <constant name="Infinity" type="Number"/>
  <constant name="int" type="int"/>
  <constant name="String" type="String"/>
  <constant name="Object" type="Object"/>
  <constant name="HIDE_NSURI_METHODS" type="uint" uri="avmplus"/>
  <constant name="INCLUDE_BASES" type="uint" uri="avmplus"/>
  <constant name="Array" type="Array"/>
  <constant name="INCLUDE_VARIABLES" type="uint" uri="avmplus"/>
  <constant name="INCLUDE_ACCESSORS" type="uint" uri="avmplus"/>
  <constant name="INCLUDE_INTERFACES" type="uint" uri="avmplus"/>
  <constant name="INCLUDE_METHODS" type="uint" uri="avmplus"/>
  <constant name="INCLUDE_METADATA" type="uint" uri="avmplus"/>
  <constant name="INCLUDE_CONSTRUCTOR" type="uint" uri="avmplus"/>
  <constant name="INCLUDE_TRAITS" type="uint" uri="avmplus"/>
  <constant name="Class" type="Class"/>
  <constant name="HIDE_OBJECT" type="uint" uri="avmplus"/>
  <constant name="FLASH10_FLAGS" type="uint" uri="avmplus"/>
  <constant name="AS3" type="*"/>
  <constant name="Function" type="Function"/>
  <constant name="NaN" type="Number"/>
  <method name="parseInt" declaredBy="global" returnType="Number">
    <parameter index="1" type="String" optional="true"/>
    <parameter index="2" type="int" optional="true"/>
  </method>
  <method name="parseFloat" declaredBy="global" returnType="Number">
    <parameter index="1" type="String" optional="true"/>
  </method>
  <method name="escape" declaredBy="global" returnType="String">
    <parameter index="1" type="String" optional="true"/>
  </method>
  <method name="unescape" declaredBy="global" returnType="String">
    <parameter index="1" type="String" optional="true"/>
  </method>
  <method name="isXMLName" declaredBy="global" returnType="Boolean">
    <parameter index="1" type="*" optional="true"/>
  </method>
  <method name="describeType" declaredBy="global" returnType="XML" uri="avmplus">
    <parameter index="1" type="*" optional="false"/>
    <parameter index="2" type="uint" optional="false"/>
  </method>
  <method name="getQualifiedClassName" declaredBy="global" returnType="String" uri="avmplus">
    <parameter index="1" type="*" optional="false"/>
  </method>
  <method name="getQualifiedSuperclassName" declaredBy="global" returnType="String" uri="avmplus">
    <parameter index="1" type="*" optional="false"/>
  </method>
  <method name="decodeURI" declaredBy="global" returnType="String">
    <parameter index="1" type="String" optional="true"/>
  </method>
  <method name="decodeURIComponent" declaredBy="global" returnType="String">
    <parameter index="1" type="String" optional="true"/>
  </method>
  <method name="encodeURI" declaredBy="global" returnType="String">
    <parameter index="1" type="String" optional="true"/>
  </method>
  <method name="encodeURIComponent" declaredBy="global" returnType="String">
    <parameter index="1" type="String" optional="true"/>
  </method>
  <method name="isNaN" declaredBy="global" returnType="Boolean">
    <parameter index="1" type="Number" optional="true"/>
  </method>
  <method name="isFinite" declaredBy="global" returnType="Boolean">
    <parameter index="1" type="Number" optional="true"/>
  </method>
</type>:
*/

description = flash.utils.describeType( Object( this ).constructor as Class );
trace( "description:\n" + description );

/* outputs :
<type name="Object" base="Class" isDynamic="true" isFinal="true" isStatic="true">
  <extendsClass type="Class"/>
  <extendsClass type="Object"/>
  <constant name="length" type="int"/>
  <accessor name="prototype" access="readonly" type="*" declaredBy="Class"/>
  <factory type="Object"/>
</type>:
*/
跟踪(此);//输出:[对象全局]
trace(flash.utils.descripbetype(this));
/*产出:
说明:
:
*/
description=flash.utils.describeType(对象(this).constructor作为类);
跟踪(“说明:\n”+说明);
/*产出:
:
*/

很高兴有人提到全局变量语法(:哇;我不知道它在那里。非常有用。很好用:)