vendredi 25 novembre 2016

If I give you a reference to the function can you get the name of the function?

I'm writing debugging tools and I want to make it clear where I'm at. My ideal output would write the class name and function name to the console like so:

Vehicle.addWheels()
  Wheels were added by the user

and in code it would be a function like:

trace(addWheels, "Wheels were added by the user");

I'm thinking of writing a custom function that gets the details like so (pseudo code):

public static myTrace(function:Function, message:String):void {
   var className:String = function.parent;
   var functionName:String = getQualifiedName(function.prototype);
   trace(className + "." + functionName + ": " + message);
}

This is an AS3 example but AS3 is based on JavaScript so if it works in Javascript it may work in ActionScript.

If that doesn't work I can pass in the class reference easily using a third parameter but still need to know how to get the name of the function.

public static myTrace(object:Object, function:Function, message:String):void {
   var className:String = getQualifiedName(object);
   var functionName:String = getQualifiedName(function.prototype);
   trace(className + "." + functionName + ": " + message);
}

Aucun commentaire:

Enregistrer un commentaire