如何在现有的flex对象中添加其他名称 - 值对

时间:2012-04-05 19:51:00

标签: flex object

人, 我有一个关于在Flex中为现有对象添加其他名称 - 值对的问题;

我有一个Object Type的object1 我宣布一个新的object2 = object1加上一些额外的名称 - 值对, 有没有办法将新的名称 - 值对附加到现有对象?

非常感谢,

1 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题,object2是object1的引用指针;因此,对object2的更改会影响object1:

package
{
    import flash.display.Sprite;

    public class X extends Sprite
    {
        public function X()
        {
            var object1:Object = {a: "1", b: "2"};

            var object2:Object = object1;

            object2.c = 3;

            trace("Object 2:");
            for (var name:String in object2)
            {
                trace("Name: " + name + " = " + object2[name]);
            }

            trace("Object 1:");
            for (var name:String in object1)
            {
                trace("Name: " + name + " = " + object1[name]);
            }
        }
    }
}

......输出:

Object 2:
Name: a = 1
Name: c = 3
Name: b = 2
Object 1:
Name: a = 1
Name: c = 3
Name: b = 2

如果您在询问是否可以克隆对象或动态类,可以使用如下函数:

public function cloneObject(object:Object):Object
{
    var newObject:Object = {};

    for (var name:String in object)
    {
        newObject[name] = object[name];
    }

    return newObject;
}