A while ago I wanted to find out if you could define your own global functions in ActionScript 3.0. After a little searching I found an answer: use static members. Sure, its a global, but it didn’t quite answer my question, what if I wanted a global function like one of the built-in ones like ‘trace’ or ‘setInterval’? At the time it seemed like no one had even thought about it.

Well if AS3 had support for user defined global functions(and why shouldn’t it? the AS3 API contains several)  it should be fairly easy to guess the syntax. So i wrote something like this:

package
{
  public function foo( ):void
  {
    trace( "bar" );
  }
}

and put it into a file called “foo.as”. And waddaya know, it worked! Also found out it worked for variables and constants, like so:

package
{
  public var someVar:int = 7;
}
package
{
  public const someConst:String = "baz";
}

Just make sure the file name matches the variable and that its in the correct source path.

I later discovered that Java does not support global standalone functions and variables. Since AS3 is influenced by Java maybe some people assume it would be the same.