我期待一些讽刺,但是到底是什么。我搜索了actionscript参考页面,似乎无法找到如何声明简单的全局变量。如何在actionscript中声明一个全局变量
回答
已经有好几个好的答案了。我只想指出YOU CAN
在AS3中创建全局变量。只需在您的类文件夹的根目录创建一个文件,例如,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”值显示两次。
as3中没有全局变量。但是可以在不创建类的实例的情况下访问静态变量,因此可以将它们用作全局变量。
MyClass.myStaticVar = 5;
// ...
var value:Number = MyClass.myStaticVar;
全球什么:
package
{
class MyClass
{
// ...
public static var myStaticVar: Number;
// ...
}
}
然后在你的程序中的任何地方,你可以像如下方式访问变量myStaticVar?
如果你想声明一个类是“全局”的变量,你可以使用任何变量:
public var myClassGlobal : Object = new Object();
这个变量可以在类内的任何地方访问;并且因为我公开它也适用于任何可以访问该类的实例的类。您将访问它像这样:
public var myFlexApplicationGlobal :Object = new Object():
您:
trace(myClassInstance.myClassGlobal);
如果要声明一个变量,这是“全球性”的Flex应用程序,你可以在主应用程序文件中声明一个变量可以使用FlexGlobals.topLevelApplication在代码中的任何位置访问此值。像这样的:
trace((FlexGlobals.topLevelApplication as myMainApplicationFile).myFlexApplicationGlobal);
这样的一种方法通常被认为是封装no-no;因为它为您的类提供了对主应用程序文件的依赖并最大限度地减少了重用。这通常是我们试图避免的。
您可以创建一个静态变量并使用该类在任何地方访问它。静态变量是不依赖于类的一个实例:
public static var myStaticGlobal :Object = new Object():
您将访问它像这样:
trace(MyClassWithStaticVariable.myStaticGlobal);
您还可以创建一个Singleton类与全局变量和使用的框架,例如作为Swiz或RobotLegs将该类注入需要它的类中。快速谷歌搜索应该揭示你在Flex中创建单例的信息;并且在更大的编程社区范围内有许多针对单身人士的讨论。
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]
}
从方法闭合关键字this
内返回到“全局”对象的引用。
下面是一些有趣的结果,当检查this
从内方法封闭。
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>:
*/
- 1. 在ActionScript中声明全局变量?
- 2. 在全局头文件中声明一个全局变量?
- 3. 声明一个全局的Request.ServerVariable变量
- 4. 我如何在AngularJs中声明一个全局变量2
- 5. 如何在php中声明一个全局变量?
- 6. 如何在ASP.NET MVC中声明一个全局变量页面
- 7. 如何在Objective-C中声明一个全局变量?
- 8. 声明全局变量
- 9. 全局变量声明
- 10. JavaScript;我如何声明变量全局?
- 11. 如何声明全局变量?
- 12. 如何声明全局变量
- 13. 如何正确声明全局变量?
- 14. 在Arduino中声明全局变量
- 15. 在perl中声明全局变量
- 16. 在Matlab中声明全局变量
- 17. 在coffescript中声明全局变量
- 18. 在yacc中声明全局变量
- 19. 全局变量在android中的声明
- 20. 在Java中声明全局变量
- 21. 在全局变量中声明查询
- 22. 在Python中声明全局变量类
- 23. 在Swift中声明全局变量
- 24. 在vb中声明全局变量
- 25. 哪一个更好,在类中声明全局变量或在Java类中声明局部变量?
- 26. 在python中声明一个函数中的全局变量
- 27. 在JavaScript中声明一个临时变量到一个全局变量responseText
- 28. 如何从类内声明一个全局变量?
- 29. 如何从函数内部声明一个全局变量?
- 30. 声明一个全局变量用作本地变量
高兴有人提到的全局变量语法(: – JonnyReeves 2012-02-29 20:15:04
哇,我不知道这是有 – JeffryHouser 2012-02-29 20:43:55
非常有用的工作得很好:)。 – 2015-08-28 10:15:29