Can I take a variable name and turn it into a string in ActionScript 3.0?

I create the simplest debugger window in ActionScript for myself, where I can add and remove variables that I want to track. I had to add the variables to the list just by doing something like

DebuggerMonitor.trackVar(variable).

My question is: is there a way to turn a "variable" (name, not value) into a string to be added to the text box?

+3
source share
3 answers

Depending on how smart your debugger should be, you can simply pass the name:

DebuggerMonitor.trackVar( variable, "variable" );  

since, obviously, when used in such a context, the name should be known at the time of writing the program.

, , ( ):

public function getVariableName( instance:*, match:* ):String {
    var typeDescription:XML = describeType( instance );
    var variables:XMLList = typeDescription..variable;
    var accessors:XMLList = typeDescription..accessor;
    for each(var variable:XML in variables) 
        if(matchesXMLName( instance, variable, match )) 
            return variable.@name;
    for each(var accessor:XML in accessors) 
        if(matchesXMLName( instance, accessor, match )) 
            return accessor.@name;
    return "No name found.";
}

private function matchesXMLName( instance:*, xml:XML, match:* ):Boolean {
    return match == instance[xml.@name.toString()];
}

var varName:String = getVariableName ( myObject, variable );

, - .

as3commons reflections package - ...

+2

- : (

, , .

0

, "var" , .

  • ( "var" ).

  • ( "var" ).

, . , , , , "" .

- " API" , , , , , . ActionScript LHS, , , , . describeType , . , describeType , .

0

All Articles