get string representation of a variable name in as3

后端 未结 4 1568
日久生厌
日久生厌 2020-12-22 10:52

any way of doing this is as3?

for example, if I have a var dog:String, how can I get \"dog\" out of that variable?

Looking into reflection to d

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 11:27

    In your example I don't think there is a way to retrieve "dog" as a String.

    However, if dog is a property of a dynamic Object, then you could use a function like this:

    function getVarName(subject:*, value:*):String
    {
        for(var i:String in subject)
        {
            if(subject[i] == value) return i;
        }
    
        return "";
    }
    

    This function can work in a scenario like this:

    var holder:Object = {
        dog: "some awesome dog"
    }
    
    trace(getVarName(holder, "some awesome dog")); // dog
    

提交回复
热议问题