开发者

Air/Flex concatenating a variable with a property

开发者 https://www.devze.com 2022-12-25 21:38 出处:网络
I have three text boxes on the stage id=red, blue, green same as the keys in my cars Object/Array <?xml version=\"1.0\" encoding=\"utf-8\"?>

I have three text boxes on the stage id=red, blue, green same as the keys in my cars Object/Array

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="carsToBox开发者_高级运维()">
    <fx:Script>
        <![CDATA[
            public function carsToBox():void{

                var cars:Object={red:"300zx",blue:"Skyline",green:"Supra"};
                    for(var tempObj:String in cars)
                    {
                        tempObj.text= cars[tempObj];
                    }
            }       
        ]]>
    </fx:Script>

    <s:TextInput x="65" y="53" id="red"/>
    <s:TextInput x="64" y="88" id="blue"/>
    <s:TextInput x="64" y="118" id="green"/>
</s:WindowedApplication>

So I'm thinking "tempObj.text" would equal red.text but I can't stick "tempObj" with ".text" is there a way this can be done?


Basically your tempObj is actually the property names of the cars object. I've renamed it to prop for clarity. As the property names of cars is the same os the property names of the textInput controls you can use that name like so:

<?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="carsToBox()">
        <fx:Script>
            <![CDATA[
                public function carsToBox():void
                {

                    var cars:Object={red:"300zx",blue:"Skyline",green:"Supra"};
                    for(var prop:String in cars)
                    {
                        this[prop].text = cars[prop]
                    }
                }       
            ]]>
        </fx:Script>

        <s:TextInput x="65" y="53" id="red"/>
        <s:TextInput x="64" y="88" id="blue"/>
        <s:TextInput x="64" y="118" id="green"/>
    </s:WindowedApplication>
0

精彩评论

暂无评论...
验证码 换一张
取 消