How to access top level package in ActionScript?

2019-05-04 11:53发布

I would like to access an ActionScript 3.0 top level function from a class of mine, in which a symbol with the same name (trace) as the top level function is already defined:

class A {
  public static function trace():void {
  }
  trace("Test");
}

I would like to call the global ActionScript trace function with trace("Test"), but this is not possible as another symbol function trace() is defined.

In a case where the external definition which I would like to access would be located in a package (flash.utils or so), I could access that external definition with flash.utils.[definitionName], as I do here with ByteArray:

import flash.utils.*;
class ByteArray {
  var data:flash.utils.ByteArray;
}

Is there a similar syntactical expression in ActionScript 3.0 that would allow me to access the trace function in the first example without changing the name of my class method trace?

This is a hypothetical question, please do not consider workarounds. I need to solve the problem exactly as asked above. Thanks in advance!

1条回答
我想做一个坏孩纸
2楼-- · 2019-05-04 12:53

trace is in the public namespace so it could be reached calling public::trace , however here the problem is that you are redefining another public trace, so you can't call the previous one.

What you can do is :

1 - if your method trace have not to be public made it protected or private and then you will be able to call the original trace:

public class Main extends Sprite 
{
    protected function trace(...args):void {
        public::trace(args) // access to the public function trace
    }
    public function Main():void 
    {
        trace("hello world")
    }       
}

2 - if you can't change the signature assign the original trace into a static var/const so you can use it later :

public class Main extends Sprite 
{
    // here save the original trace function
    private static const _trace:Function = trace

    public function trace(...args):void {
        _trace(args) // call the original trace
    }
    public function Main():void 
    {
        trace("hello world")
    }       
}
查看更多
登录 后发表回答