forked from: Dictionary の複製の仕方を教えてください…

by shohei909 forked from Dictionary の複製の仕方を教えてください… (diff: 31)
♥0 | Line 49 | Modified 2010-11-01 19:57:37 | MIT License
play

ActionScript3 source code

/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3ahC
 */

// forked from mezumona's Dictionary の複製の仕方を教えてください…
/**
 * ByteArrayを使う方法もありますが不完全な複製しかできません。
 * forループを使うのが無難だと思います。
 */

package {
    import flash.text.TextFieldAutoSize;
    import flash.text.TextField;
    import flash.utils.Dictionary;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        private var hoge:Object = {};
        public function FlashTest() {
            initializeDisplay();
            var base:Dictionary = new Dictionary();
                base["hoge"] = "Hoge";
                base["piyo"] = "Piyo";
                base[hoge] = "Hoge2";
                
            // forループを使うクローン。
            var clone1:Dictionary = clone1(base);
            
            // ByteArrayを使うクローン(不完全)
            var clone2:Dictionary = clone2(base);
            
                base["hoge"] = "foobar";
                base[hoge] = "foobar2";
            trace(base["hoge"], clone1["hoge"], clone2["hoge"] );
            trace(base[hoge], clone1[hoge], clone2[hoge] );
        }

        private var tf_:TextField;
        public function initializeDisplay():void
        {
            var tf:TextField = new TextField();
                tf.autoSize = TextFieldAutoSize.LEFT;
            tf_ = tf;
            addChild(tf);
        }

        public function trace(...args):void
        {
            tf_.appendText("\n" + args.join(" "));
        }


    }
}
import flash.utils.Dictionary;
function clone1( arg:Dictionary, weakKeys:Boolean = false ): Dictionary{
    var cln:Dictionary = new Dictionary( weakKeys );
    for( var o:Object in arg ){ cln[o] = arg[o]; }
    return cln;
}

import flash.utils.ByteArray;    
import flash.net.registerClassAlias;
function clone2(source:Dictionary):Dictionary{
    registerClassAlias("flash.utils.Dictionary", Dictionary);
    var myBA:ByteArray = new ByteArray();
    myBA.writeObject(source);
    myBA.position = 0;
    return(myBA.readObject());
}