gpt4 book ai didi

actionscript-3 - AS3调用全局函数而没有“new”或具有静态方法

转载 作者:行者123 更新时间:2023-12-03 22:41:52 26 4
gpt4 key购买 nike

我正在创建一个用于调试/日志的实用程序函数,可以通过导入类并调用来访问

new Log(params);

或者,我将函数更改为静态函数,并将其重命名为It

然后我可以通过Log.It(params)调用它

我想知道是否可以进行设置,以便像Log(params)命令一样简单地调用trace

功能:

package 
{
public class Log
{
/** Gets the name of the function which is calling */
public function Log(prefix:String = "", suffix:String = "", params:* = null):void
{
var error:Error = new Error();
var stackTrace:String = error.getStackTrace(); // entire stack trace
var startIndex:int = stackTrace.indexOf("at ", stackTrace.indexOf("at ") + 1); //start of second line
var endIndex:int = stackTrace.indexOf("()", startIndex); // end of function name

var lastLine:String = stackTrace.substring(startIndex + 3, endIndex);
var functionSeperatorIndex:int = lastLine.indexOf('/');
var ClassSeperatorIndex:int = lastLine.indexOf(':');

var objectName:String = lastLine.substring(ClassSeperatorIndex+2, functionSeperatorIndex);
var functionName:String = lastLine.substring(functionSeperatorIndex + 1, lastLine.length);

//TODO: Loop through params

trace(prefix +" " + "[" + objectName + "]" + " > " + functionName + " " + suffix);

//TODO: Log to Array
//TODO: Dispatch Event
}
}
}

最佳答案

您可以创建包级方法(我不知道这是否正确),这是一个示例:

concat.as:

package com.example.utils 
{
public function concat(string1:String, string2:String):String
{
return string1.concat(string2);

}// end function

}// end package


Main.as(文档类):

package 
{
import com.example.utils.*;
import flash.display.Sprite;
import flash.events.Event;

public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);

}// end function

private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);

var string1:String = "Hello";
var string2:String = "World";
var string3:String = concat(string1, string2);

trace(string3); // output: HelloWorld

}// end function

}// end class

}// end package

关于actionscript-3 - AS3调用全局函数而没有“new”或具有静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7567263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com