Search code examples
dynamicclonehaxe

How to clone Dynamic object in Haxe?


I have a Dynamic object from Json and need to clone that in Haxe. Is there any easy way to clone object, please let me know. Or if it's impossible, I want at least iterate that Dynamic object such as JavaScript object.

var config = {
    loop : true,
    autoplay : true,
    path : "data.txt"
};
var newConfig = {};
for (i in config) {
    if (config.hasOwnProperty(i))
        newConfig[i] = config[i];
}

Solution

  • Use Reflect.copy():

    var newConfig = Reflect.copy(config);
    

    Note that it only guaranteed to work on anonymous structures. For other objects, use the appropriate Reflect methods.